oneflow.autograd

Functions and classes for autograd.

class oneflow.autograd.Function(self)

Base class to create custom autograd.Function.

To create a custom autograd.Function, subclass this class and implement the forward() and backward() static methods. Then, to use your custom op in the forward pass, call the class method apply() or __call__(). Do not call forward() directly.

For example:

class Exp(Function):
    @staticmethod
    def forward(ctx, i):
        result = i.exp()
        ctx.save_for_backward(result)
        return result

    @staticmethod
    def backward(ctx, grad_output):
        result, = ctx.saved_tensors
        return grad_output * result

# Use it by calling the apply method or __call__ method
output = Exp.apply(input)  # output = Exp()(input)
__call__(*inputs)

See self.apply().

classmethod apply(*inputs)

Calculate output tensors and build backward graph.

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

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.

oneflow.autograd.backward(outputs: Union[oneflow._oneflow_internal.Tensor, Sequence[oneflow._oneflow_internal.Tensor]], out_grads: Optional[Union[oneflow._oneflow_internal.Tensor, Sequence[oneflow._oneflow_internal.Tensor]]], retain_graph: bool = False, create_graph: bool = False)None
oneflow.autograd.grad(outputs: Union[oneflow._oneflow_internal.Tensor, Sequence[oneflow._oneflow_internal.Tensor]], inputs: Union[oneflow._oneflow_internal.Tensor, Sequence[oneflow._oneflow_internal.Tensor]], out_grads: Optional[Union[oneflow._oneflow_internal.Tensor, Sequence[oneflow._oneflow_internal.Tensor]]] = None, retain_graph: bool = False, create_graph: bool = False)Tuple[oneflow._oneflow_internal.Tensor]