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

replace deprecated np.bool #9649

Merged
merged 6 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion python/oneflow/framework/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def convert_proto_dtype_to_oneflow_dtype(proto_dtype):


_ONEFLOW_DTYPE_TO_NUMPY_DTYPE = {
oneflow.bool: np.bool,
# >> np_bool = np.array([1,2], dtype=bool).dtype
# >> np_bool == bool
# True
oneflow.bool: bool,
oneflow.float: np.float32,
oneflow.float16: np.float16,
oneflow.float32: np.float32,
Expand Down
45 changes: 45 additions & 0 deletions python/oneflow/test/misc/test_np_dtype_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
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 unittest
import numpy as np
import oneflow as flow
import oneflow.unittest


@unittest.skipIf(os.getenv("ONEFLOW_TEST_CPU_ONLY"), "only test cpu cases")
@flow.unittest.skip_unless_1n1d()
class TestNpDtypeConverter(flow.unittest.TestCase):
def test_np_dtype_converter(test_case):
for flow_dtype in flow.dtypes():
if flow_dtype in [flow.record, flow.tensor_buffer, flow.bfloat16]:
continue
np_dtype = flow.convert_oneflow_dtype_to_numpy_dtype(flow_dtype)
test_case.assertEqual(
flow.framework.dtype.convert_numpy_dtype_to_oneflow_dtype(np_dtype),
flow_dtype,
)

# Test whether dtype conversion works with arr.dtype
np_arr = np.array([1, 2], dtype=np_dtype)
test_case.assertEqual(np_arr.dtype, np_dtype)
flow_tensor = flow.tensor([1, 2], dtype=flow_dtype)
test_case.assertEqual(flow_tensor.dtype, flow_dtype)


if __name__ == "__main__":
unittest.main()

1 change: 0 additions & 1 deletion python/oneflow/test/modules/test_as_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
np.int64: flow.int64,
np.int8: flow.int8,
np.uint8: flow.uint8,
np.bool: flow.bool,
np.float64: flow.float64,
np.float32: flow.float32,
np.float16: flow.float16,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _test_fused_scale_mask_softmax(
if broadcast_dim:
mask_size[broadcast_dim] = 1

mask = np.random.randint(0, 2, size=mask_size, dtype=np.bool)
mask = np.random.randint(0, 2, size=mask_size, dtype=bool)
fused_x_tensor = flow.tensor(x, dtype=flow.float32).to("cuda")
fused_mask_tensor = flow.tensor(mask, dtype=flow.bool).to("cuda")
fused_x_tensor.requires_grad = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _test_fused_scale_mask_softmax_dropout(
mask_size = [batch_size, num_heads, seq_length, seq_length]
if broadcast_dim:
mask_size[broadcast_dim] = 1
mask = np.random.randint(0, 2, size=mask_size, dtype=np.bool)
mask = np.random.randint(0, 2, size=mask_size, dtype=bool)

fused_x_tensor = flow.tensor(x, dtype=flow.float32).to("cuda")
fused_mask_tensor = flow.tensor(mask, dtype=flow.bool).to("cuda")
Expand Down
2 changes: 1 addition & 1 deletion python/oneflow/test/tensor/test_tensor_part_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_tensor_equal(test_case):
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))

def test_tensor_equal_bool_dtype(test_case):
np_bool = np.random.randint(0, 2, size=()).astype(np.bool).item()
np_bool = np.random.randint(0, 2, size=()).astype(bool).item()
input = flow.tensor(np_bool, dtype=flow.bool)
input2 = flow.tensor([np_bool], dtype=flow.bool)
test_case.assertTrue(input == np_bool)
Expand Down