-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add python interface design #3964
Conversation
doc/design/python_interface.md
Outdated
# Python Interface Design | ||
|
||
## Variable as variable data type (not parameter) | ||
The class Variable is introduced to define the variables that are not parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class Variable is introduced to define the variables, which are not only parameters but also the input data and intermediate outputs.
doc/design/python_interface.md
Outdated
## Variable as variable data type (not parameter) | ||
The class Variable is introduced to define the variables that are not parameters. | ||
|
||
All the variables are defined in a variable scope, `VarScope`, it will check duplicate variable names. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that Scope
is enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this design is based on current implementation ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really. This design should handle current implementation and future compile time concepts.
However, is the compile time concept of Scope
needs a different name? because in that time, the user cannot access runtime scope directly, so there is only one concept which related to scope in future?
doc/design/python_interface.md
Outdated
|
||
The `Variable`'s definition is as follows | ||
```python | ||
class _unique_name_generator: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8, class name is upper case
class UniqueNameGenerator(object):
...
doc/design/python_interface.md
Outdated
counter = 0 | ||
|
||
def __call__(self, prefix): | ||
name = '%s-%d' % (prefix, counter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe %s.%d
is better. Because %s-%d
is not a valid filename on some platforms, e.g., Windows.
doc/design/python_interface.md
Outdated
''' | ||
var_scope is VarDesc's scope. | ||
''' | ||
self.shape = shape |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shape
is not stored in Variable. It stored in tensor.
doc/design/python_interface.md
Outdated
name = unique_name_generator("unknown") | ||
self.name = name | ||
self.var_scope = var_scope | ||
if not reuse: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may not be Variable
's responsibility to check reuse
.
doc/design/python_interface.md
Outdated
g_varscope_stack = [] | ||
|
||
|
||
class VarScope(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VarScope
may not be exposed to users. It should be managed with sub-block
or sub-net
.
So, let us not write it in this design doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some code snippets, not the actual definition. Explain something about a variable scope, no matter what its name is, Scope or VarScope.
Even the original Scope is used, it is better to use a python adapter may be called VarScope
so that when we replace Scope
easily in Variable latter.
doc/design/python_interface.md
Outdated
So, the parameters should only created in the head of the variable stack, we can implement this stack like | ||
|
||
```python | ||
g_varscope_stack = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
global variables are not good, especially for multi-thread situation.
Maybe we can,
class ScopeStack(object):
def __init__(self):
self.scope = [Scope()]
def push_stack(self):
self.scope.push(Scope())
def pop_stack(self):
self.scope.pop()
def cur_scope(self):
return self.scope[-1]
def global_scope(self):
return self.scope[0]
g_scope_stack = ScopeStack()
# `new_variable` could be based on the `g_scope_stack`, and could be based on a user-defined scope stack.
def new_variable(scope_stack=None):
if scope_stack is None:
scope_stack = g_scope_stack
return scope_stack.cur_scope.new()
doc/design/python_interface.md
Outdated
# Python Interface Design | ||
|
||
## Variable as variable data type (not parameter) | ||
The class Variable is introduced to define the variables, which are not only parameters but also the input data and intermediate outputs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class Variable
is ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get
''' | ||
if name is None: | ||
if prefix is not None: | ||
name = unique_name_generator(prefix) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this design. We don't need to call unique_name_generator
in layer definitions.
name = unique_name_generator("unknown") | ||
self.name = name | ||
self.var_scope = var_scope | ||
self.op = op |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to define a varibale
's op
? If two operators write the same variable, which one is its op
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the last one who write it? @reyoung @jacquesqiao
doc/design/python_interface.md
Outdated
in above example, the `unique_name_generator` is introduced to make unique name with a prefix and can be used to generate variables' and operators' names. | ||
|
||
## Scope Stack for Block Inherience | ||
Each block should has a variable scope that is inheriented from parent's variable scope. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should have. 情态动词后不用第三人称单数形式。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get
g_scope_stack = ScopeStack() | ||
|
||
|
||
# `new_variable` could be based on the `g_scope_stack`, and could be based on a user-defined scope stack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users are able to build their own scope stack? What's that for?
No description provided.