Open Tongvivi-0418 opened 1 year ago
你可以帮忙新写一个文档么,使用方式已经简化了很多
参考这里:https://github.com/Oneflow-Inc/oneflow/pull/9984
share 相关的逻辑都不用自己写了,只用加一行代码就可以支持。
https://github.com/Oneflow-Inc/diffusers/wiki/Optimization-for-Multi-Resolution-Picture
可以按你的已经,整个 refine 一遍。
graph 层面的,可以参考上面的文章的风格写个说明文档。参考这里:https://github.com/Oneflow-Inc/oneflow/pull/9984
define graph (with dynamic input shape)
import oneflow as flow
class LinearGraph(flow.nn.Graph):
# [New] use this decorator to enable dynamic input shape 【原始的静态图,加上这一行,就可以执行输入的动态 shape 了】
@flow.nn.Graph.with_dynamic_input_shape()
def __init__(self):
super().__init__()
self.my_linear = flow.nn.Linear(3, 8, False)
def build(self, x):
return self.my_linear(x)
init and call graph (with dynamic input shape)
linear_g = LinearGraph()
# x_with_shape0 and x_with_shape1 can have different shape
linear_g(x_with_shape0)
linear_g(x_with_shape1)
config cache size【下层实现是多图缓存的机制,支持设置缓存大小,默认是10】
define graph (with dynamic input shape)
```python
import oneflow as flow
class LinearGraph(flow.nn.Graph):
# [New] config cache size with size parameter
@flow.nn.Graph.with_dynamic_input_shape(size=8)
def __init__(self):
super().__init__()
self.my_linear = flow.nn.Linear(3, 8, False)
def build(self, x):
return self.my_linear(x)
【另外支持保存和加载编译好的graph,加载 graph 可以直接执行无需编译】 save
class LinearGraph(flow.nn.Graph):
@flow.nn.Graph.with_dynamic_input_shape()
def __init__(self):
# [New]enable get runtime_state_dict
super().__init__(enable_get_runtime_state_dict=True)
self.my_linear = flow.nn.Linear(3, 8, False)
def build(self, x):
return self.my_linear(x)
linear_g = LinearGraph()
# call graph
linear_g(x_with_shape0)
linear_g(x_with_shape1)
# [New]save graph (with dynamic input shape)
state_dict = linear_g.runtime_state_dict()
flow.save(state_dict, filename)
load
linear_g = LinearGraph()
state_dict = flow.load(filename)
# [New]load graph(with dynamic input shape)
linear_g.load_runtime_state_dict(state_dict)
# call graph
linear_g(x_with_shape0)
linear_g(x_with_shape1)
@strint 请问我是直接在原文档edit吗,还是重开一个新文档~这个pipe层面和graph层面我没太理解,所以不太清楚具体要做什么
在OneFlow的nn.Graph中,使用with_dynamic_input_shape()装饰器可以启用动态输入形状。这样可以在运行时根据输入张量的大小自动确定图的形状。
对于每个新的形状大小,Graph都会重新编译并缓存编译结果,之后遇到同样的输入形状,可以直接复用编译结果。多个缓存的编译图会做内存共享。缓存的图数目默认为10,可以通过size参数进行配置。
以下是一个示例:
import oneflow as flow
class LinearGraph(flow.nn.Graph):
# [New] 使用 with_dynamic_input_shape() 启用动态输入形状
@flow.nn.Graph.with_dynamic_input_shape()
def __init__(self):
super().__init__()
self.my_linear = flow.nn.Linear(3, 8, False)
def build(self, x):
return self.my_linear(x)
linear_g = LinearGraph()
# x_with_shape0 和 x_with_shape1 可以有不同的形状大小
linear_g(x_with_shape0)
linear_g(x_with_shape1)
# [New] 使用 size 参数配置缓存大小
class LinearGraph(flow.nn.Graph):
# [New] 使用 with_dynamic_input_shape() 启用动态输入形状并配置缓存大小
@flow.nn.Graph.with_dynamic_input_shape(size=8)
def __init__(self):
super().__init__()
self.my_linear = flow.nn.Linear(3, 8, False)
def build(self, x):
return self.my_linear(x)
OneFlow的nn.Graph支持保存已编译的图,以便以后可以直接加载执行,而不需要重新编译。可以使用runtime_state_dict()方法获取已编译图的状态字典,以便在以后重新加载它。
以下是一个保存和加载已编译图的示例:
class LinearGraph(flow.nn.Graph):
@flow.nn.Graph.with_dynamic_input_shape()
def __init__(self):
# [New] 启用 get_runtime_state_dict()
super().__init__(enable_get_runtime_state_dict=True)
self.my_linear = flow.nn.Linear(3, 8, False)
def build(self, x):
return self.my_linear(x)
linear_g = LinearGraph()
# 调用图
linear_g(x_with_shape0)
linear_g(x_with_shape1)
# [New] 保存已编译的图
state_dict = linear_g.runtime_state_dict()
flow.save(state_dict, filename)
# [New] 加载已编译的图
linear_g = LinearGraph()
state_dict = flow.load(filename)
linear_g.load_runtime_state_dict(state_dict)
# 调用图
linear_g(x_with_shape0)
linear_g(x_with_shape1)
@strint 请问我是直接在原文档edit吗,还是重开一个新文档~这个pipe层面和graph层面我没太理解,所以不太清楚具体要做什么
新开一个地方编辑吧。具体可以问下 shenghang 和 yaochi
建议
header 层级选择错误:problems和solutions应该属于同一层级,一一对应;以及下面这种形式会不会更好?
problem 1...
solution 1
problem 2
solution 2
步骤若有前后顺序,不要使用无序列表,改为有序列表
同级Header尽量采用相似格式:Compile and share between... 对应 Load the compiled result,删除How to,更简洁清晰
单句优化 After opening, multiple graphs with different input shapes but the same parameters can be shared:
缩写尽量大写,如 cpu-CPU
语言优化
疑惑