Skip to content

Commit

Permalink
Merge pull request #14 from yuguo-Jack/rocm
Browse files Browse the repository at this point in the history
merge master
  • Loading branch information
levi131 authored Mar 15, 2024
2 parents 2f8fb3b + a0ef36b commit 9605ae1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ OneFlow 是一个深度学习框架,旨在**易用,可扩展且高效**。

## Latest News

- Version 0.9.0 is out!
- [Full changelog](https://github.com/Oneflow-Inc/oneflow/releases/tag/v0.9.0)
- Version 1.0.0 is out!
- [Full changelog](https://github.com/Oneflow-Inc/oneflow/releases/tag/v1.0.0)

## 安装 OneFlow-DCU

Expand Down
39 changes: 5 additions & 34 deletions python/oneflow/framework/infer_compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,14 @@
limitations under the License.
"""

import os

import oneflow as flow
from oneflow.framework.args_tree import ArgsTree
try:
import torch
except ImportError:
print("You should install torch also when use `oneflow.framework.infer_compiler`.")

from .transform.custom_transform import register
from .utils.patch_for_compiler import *
from .with_fx_graph import fx_node_tranform
from .with_fx_interpreter import OneFlowInterpreter
from .with_oneflow_compile import compile_from_torch


def oneflow_backend(gm, example_inputs, *args, **kwargs):
with_interp = os.getenv(
"ONEDIFF_INFER_COMPILER_USE_INTERPRETER", "False"
).lower() in ("true", "1", "t",)
if not with_interp:
transformed_fn = fx_node_tranform(gm)

def wrapped_forward(*args, **kwargs):
def input_fn(value):
if isinstance(value, torch.Tensor):
return flow.utils.tensor.from_torch(value.contiguous())
else:
return value

args_tree = ArgsTree((args, kwargs), False, tensor_type=torch.Tensor)
out = args_tree.map_leaf(input_fn)
args = out[0]
if with_interp:
output = OneFlowInterpreter(gm, garbage_collect_values=False).run(
*args, **kwargs
)
else:
output = transformed_fn(*args, **kwargs)
if isinstance(output, tuple):
return tuple(flow.utils.tensor.to_torch(i) for i in output)
return flow.utils.tensor.to_torch(output)

return wrapped_forward
from .with_oneflow_backend import oneflow_backend
2 changes: 1 addition & 1 deletion python/oneflow/framework/infer_compiler/with_fx_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def fx_node_tranform(gm):
os.environ.setdefault("ONEFLOW_MLIR_FUSE_FORWARD_OPS", "1")
os.environ.setdefault("ONEFLOW_MLIR_FUSE_OPS_WITH_BACKWARD_IMPL", "1")
os.environ.setdefault("ONEFLOW_MLIR_GROUP_MATMUL", "1")
os.environ.setdefault("ONEFLOW_MLIR_PREFER_NHWC", "1")
os.environ.setdefault("ONEFLOW_MLIR_PREFER_NHWC", "0")
os.environ.setdefault("ONEFLOW_KERNEL_ENABLE_FUSED_CONV_BIAS", "1")
os.environ.setdefault("ONEFLOW_KERNEL_ENABLE_FUSED_LINEAR", "1")
os.environ.setdefault(
Expand Down
53 changes: 53 additions & 0 deletions python/oneflow/framework/infer_compiler/with_oneflow_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import os
import torch

import oneflow as flow
from oneflow.framework.args_tree import ArgsTree
from .with_fx_graph import fx_node_tranform
from .with_fx_interpreter import OneFlowInterpreter


def oneflow_backend(gm, example_inputs, *args, **kwargs):
with_interp = os.getenv(
"ONEDIFF_INFER_COMPILER_USE_INTERPRETER", "False"
).lower() in ("true", "1", "t",)
if not with_interp:
transformed_fn = fx_node_tranform(gm)

def wrapped_forward(*args, **kwargs):
def input_fn(value):
if isinstance(value, torch.Tensor):
return flow.utils.tensor.from_torch(value.contiguous())
else:
return value

args_tree = ArgsTree((args, kwargs), False, tensor_type=torch.Tensor)
out = args_tree.map_leaf(input_fn)
args = out[0]
if with_interp:
output = OneFlowInterpreter(gm, garbage_collect_values=False).run(
*args, **kwargs
)
else:
output = transformed_fn(*args, **kwargs)
if isinstance(output, tuple):
return tuple(flow.utils.tensor.to_torch(i) for i in output)
return flow.utils.tensor.to_torch(output)

return wrapped_forward
61 changes: 40 additions & 21 deletions python/oneflow/test/misc/test_autograd_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from packaging import version
import unittest
import oneflow as flow
import oneflow.unittest
Expand Down Expand Up @@ -110,44 +111,62 @@ def test_hvp(test_case):
)
result_tensors = torch.autograd.functional.hvp(_func_multi_scalar, inputs, v)

# TODO: The local test of test_jacobian and test_hessian passed, but the ci test failed
"""
# TODO: "'jacobian' and 'hessian' has no strategy parameter in PyTorch before '1.11.0'"
@autotest(n=1, check_graph=False)
def test_jacobian(test_case):
inputs = random_tensor(ndim=2, dim0=5, dim1=5)
result_tensor = torch.autograd.functional.jacobian(
_func_tensor, inputs, vectorize=False, strategy="reverse-mode"
)
if version.parse(torch.pytorch.__version__) < version.parse("1.11.0"):
result_tensor = torch.autograd.functional.jacobian(
_func_tensor, inputs, vectorize=False
)
else:
result_tensor = torch.autograd.functional.jacobian(
_func_tensor, inputs, vectorize=False, strategy="reverse-mode"
)

inputs = (
random_tensor(ndim=2, dim0=5, dim1=5),
random_tensor(ndim=2, dim0=5, dim1=5),
)
result_tensors = torch.autograd.functional.jacobian(
_func_multi_scalar, inputs, vectorize=False, strategy="reverse-mode"
)
if version.parse(torch.pytorch.__version__) < version.parse("1.11.0"):
result_tensors = torch.autograd.functional.jacobian(
_func_multi_scalar, inputs, vectorize=False
)
else:
result_tensors = torch.autograd.functional.jacobian(
_func_multi_scalar, inputs, vectorize=False, strategy="reverse-mode"
)

@autotest(n=1, check_graph=False)
def test_hessian(test_case):
inputs = random_tensor(ndim=2, dim0=5, dim1=5)
result_tensor = torch.autograd.functional.hessian(
_func_scalar,
inputs,
vectorize=False,
outer_jacobian_strategy="reverse-mode",
)
if version.parse(torch.pytorch.__version__) < version.parse("1.11.0"):
result_tensor = torch.autograd.functional.hessian(
_func_scalar, inputs, vectorize=False,
)
else:
result_tensor = torch.autograd.functional.hessian(
_func_scalar,
inputs,
vectorize=False,
outer_jacobian_strategy="reverse-mode",
)

inputs = (
random_tensor(ndim=2, dim0=5, dim1=5),
random_tensor(ndim=2, dim0=5, dim1=5),
)
result_tensors = torch.autograd.functional.hessian(
_func_multi_scalar,
inputs,
vectorize=False,
outer_jacobian_strategy="reverse-mode",
)
"""
if version.parse(torch.pytorch.__version__) < version.parse("1.11.0"):
result_tensors = torch.autograd.functional.hessian(
_func_multi_scalar, inputs, vectorize=False,
)
else:
result_tensors = torch.autograd.functional.hessian(
_func_multi_scalar,
inputs,
vectorize=False,
outer_jacobian_strategy="reverse-mode",
)


if __name__ == "__main__":
Expand Down

0 comments on commit 9605ae1

Please sign in to comment.