Tongvivi-0418 / doc-review

0 stars 0 forks source link

(doc reveiw) Optimization for Multi Resolution Picture #1

Open Tongvivi-0418 opened 1 year ago

Tongvivi-0418 commented 1 year ago

建议

  1. header 层级选择错误:problems和solutions应该属于同一层级,一一对应;以及下面这种形式会不会更好?

    • problem 1...

    • solution 1

    • problem 2

    • solution 2

  2. 步骤若有前后顺序,不要使用无序列表,改为有序列表

    1679468785477
  3. 同级Header尽量采用相似格式:Compile and share between... 对应 Load the compiled result,删除How to,更简洁清晰

    1679467267798
  4. 单句优化 After opening, multiple graphs with different input shapes but the same parameters can be shared:

    • Compile pass optimization results;
    • Constant folded parameters; 两个问题:
    • can be shared 是否应该为 can share?
    • 无序列表两个点应该都是名词形式,因此第一个compile 是否应改为 compiled?
  5. 缩写尽量大写,如 cpu-CPU

  6. 语言优化

    • Problem 1, constant folding of the newly generated tensor , there is no sharing between multiple graphs, resulting in redundant overhead. 优化建议:不要全部直接用逗号连接,无法突出问题的重点;there be 句型尽量少出现在技术文档中。 是否可以直接改为:Constant folding... and no sharing between multiple graphs results in redundant overhead.
    • For each graph whose input is different but otherwise the same, the compilation will perform constant folding, and the constant folding (改为which) will calculate the constants that have nothing to do with the input at compile time, so that (删除that) there is no need to calculate at runtime. The problem is that a new tensor will be generated, and if each graph generates its own new tensor, there will be redundancy between multiple graphs. (修改为:Each graph generating its own new tensor leads to redundancy...,语言尽量简洁)
    • Problem 2, there are multiple graphs, and the compilation is started when the online service is launched, which takes a long time. 优化建议:直接点明 problem: The compilation of multiple graphs that started when the online service is launched takes a long time.
    • 9 types of input 文内input有单数和复数形式,建议统一

疑惑

  1. 为什么是 sub-problem?
  2. multi-size vs. multi-shape? 应该是一个意思?
  3. 文内 inference 的具体含义?待搜索查找
  4. tensor 张量:数据存储结构的维度
strint commented 1 year ago

你可以帮忙新写一个文档么,使用方式已经简化了很多

strint commented 1 year ago

参考这里:https://github.com/Oneflow-Inc/oneflow/pull/9984

share 相关的逻辑都不用自己写了,只用加一行代码就可以支持。

strint commented 1 year ago

pipe 层面的

https://github.com/Oneflow-Inc/diffusers/wiki/Optimization-for-Multi-Resolution-Picture

可以按你的已经,整个 refine 一遍。

graph 层面的

graph 层面的,可以参考上面的文章的风格写个说明文档。参考这里:https://github.com/Oneflow-Inc/oneflow/pull/9984

nn.Graph run with dynamic input shape by the cache.

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)

Save and load nn.Graph with dynamic input shape

【另外支持保存和加载编译好的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)
Tongvivi-0418 commented 1 year ago

@strint 请问我是直接在原文档edit吗,还是重开一个新文档~这个pipe层面和graph层面我没太理解,所以不太清楚具体要做什么

strint commented 1 year ago

nn.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)

nn.Graph的保存和加载

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 commented 1 year ago

@strint 请问我是直接在原文档edit吗,还是重开一个新文档~这个pipe层面和graph层面我没太理解,所以不太清楚具体要做什么

新开一个地方编辑吧。具体可以问下 shenghang 和 yaochi