Skip to content
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

autocast support new devices #10539

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions oneflow/api/python/framework/autocast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.
#include <pybind11/pybind11.h>
#include "oneflow/api/python/of_api_registry.h"

#include "oneflow/core/common/device_type.pb.h"
#include "oneflow/core/common/throw.h"
#include "oneflow/core/ep/include/device_manager_registry.h"
#include "oneflow/core/framework/autocast.h"

namespace py = pybind11;
Expand All @@ -36,7 +38,7 @@ class AutoCastMode {
public:
OF_DISALLOW_COPY_AND_MOVE(AutoCastMode);

AutoCastMode(const std::string& device_type, Symbol<DType> dtype, bool enabled,
AutoCastMode(const std::string& device_name, Symbol<DType> dtype, bool enabled,
bool cache_enabled)
: prev_enabled_(autocast::is_enabled()),
prev_cache_enabled_(autocast::is_autocast_cache_enabled()),
Expand All @@ -48,16 +50,23 @@ class AutoCastMode {
increase_nested_count();
autocast::set_enabled(enabled);
autocast::set_autocast_cache_enabled(cache_enabled);
if (device_type == "cpu") {
autocast::set_autocast_device_type(kCPU);
autocast::set_autocast_dtype(dtype);
autocast::set_autocast_cpu_dtype(dtype);
} else if (device_type == "cuda") {
autocast::set_autocast_device_type(kCUDA);
autocast::set_autocast_dtype(dtype);
autocast::set_autocast_gpu_dtype(dtype);
} else {
THROW(RuntimeError) << "User specified autocast device_type must be 'cuda' or 'cpu'";
auto device_type = ep::DeviceManagerRegistry::GetDeviceTypeByDeviceTypeName(device_name);
ShawnXuan marked this conversation as resolved.
Show resolved Hide resolved
switch (device_type) {
case kCPU:
autocast::set_autocast_device_type(device_type);
autocast::set_autocast_dtype(dtype);
autocast::set_autocast_cpu_dtype(dtype);
break;
case kCUDA:
case kMLU:
case kNPU:
autocast::set_autocast_device_type(device_type);
autocast::set_autocast_dtype(dtype);
autocast::set_autocast_gpu_dtype(dtype);
break;
default:
THROW(RuntimeError)
<< "User specified autocast device_type must be 'cuda' or 'cpu' or 'mlu' or 'npu'";
}
}

Expand Down
2 changes: 1 addition & 1 deletion oneflow/core/framework/autocast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ std::shared_ptr<AutoCastMeta> MakeAutoCastMeta(
// autocast only supports the following device type(s) and low precision type(s):
// - device type: CUDA
// - low precision type: half, bfloat16
static std::vector<DeviceType> autocast_device_types{kCUDA};
static std::vector<DeviceType> autocast_device_types{kCUDA, kMLU, kNPU};
static std::vector<Symbol<DType>> autocast_dtypes{DType::Float16(), DType::BFloat16()};

if (autocast_meta->autocast_color() != kBlack) {
Expand Down
5 changes: 4 additions & 1 deletion oneflow/core/job_rewriter/auto_mixed_precision_lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace oneflow {
const AMPList& AutoMixedPrecisionLists::WhiteList() {
static AMPList white_list = {"matmul",
"batch_matmul",
"conv1d",
"conv2d",
"conv3d",
"conv_data_grad",
"conv_filter_grad",
"conv_bias_grad",
Expand Down Expand Up @@ -137,7 +139,8 @@ const AMPList& AutoMixedPrecisionLists::GrayList() {
"group_norm_grad",
"silu",
"silu_grad",
"fused_weighted_sum"};
"fused_weighted_sum",
"cast"};
return gray_list;
}

Expand Down
6 changes: 3 additions & 3 deletions python/oneflow/amp/autocast_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ def __init__(
cache_enabled: Optional[bool] = None,
):
self.device = device_type
if self.device == "cuda":
self.fast_dtype = flow.get_autocast_gpu_dtype()
elif self.device == "cpu":
if self.device == "cpu":
self.fast_dtype = flow.get_autocast_cpu_dtype()
elif self.device in ["cuda", "mlu", "npu"]:
self.fast_dtype = flow.get_autocast_gpu_dtype()
else:
raise RuntimeError(
"User specified autocast device_type must be 'cuda' or 'cpu'"
Expand Down
Loading