Skip to content
Snippets Groups Projects
Unverified Commit 6638e5ba authored by Shenghang Tsai's avatar Shenghang Tsai Committed by GitHub
Browse files

Move flags from file generation to compile definition (#4173)


* port with cuda

* refactor generated_compile_flags with cuda

* use_cxx11_abi

* refactor

* larger tol

Co-authored-by: default avatarTsai <caishenghang@oneflow.org>
Co-authored-by: default avataroneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com>
parent 5e9bbfd5
No related branches found
No related tags found
No related merge requests found
......@@ -262,28 +262,8 @@ add_custom_target(of_pyscript_copy ALL
COMMAND ${CMAKE_COMMAND} -E touch "${of_pyscript_dir}/oneflow/core/__init__.py"
COMMAND ${CMAKE_COMMAND} -E make_directory "${of_pyscript_dir}/oneflow/python_gen"
COMMAND ${CMAKE_COMMAND} -E touch "${of_pyscript_dir}/oneflow/python_gen/__init__.py"
COMMAND echo "generated_compile_flags = []" > "${of_pyscript_dir}/oneflow/python_gen/sysconfig.py"
COMMAND ${Python_EXECUTABLE} "${PROJECT_SOURCE_DIR}/tools/generate_oneflow_symbols_export_file.py"
"${PROJECT_SOURCE_DIR}" "${of_pyscript_dir}/oneflow/python_gen/__export_symbols__.py")
if (BUILD_CUDA)
add_custom_command(TARGET of_pyscript_copy POST_BUILD
COMMAND echo "with_cuda=True" >> "${of_pyscript_dir}/oneflow/python_gen/compatibility.py"
COMMAND echo "\"generated_compile_flags.append('-DWITH_CUDA')\"" >> ${of_pyscript_dir}/oneflow/python_gen/sysconfig.py
)
else()
add_custom_command(TARGET of_pyscript_copy POST_BUILD
COMMAND echo "with_cuda=False" >> "${of_pyscript_dir}/oneflow/python_gen/compatibility.py")
endif()
if (USE_CXX11_ABI)
add_custom_command(TARGET of_pyscript_copy POST_BUILD
COMMAND echo "\"generated_compile_flags.append('-D_GLIBCXX_USE_CXX11_ABI=1')\"" >> ${of_pyscript_dir}/oneflow/python_gen/sysconfig.py
)
else()
add_custom_command(TARGET of_pyscript_copy POST_BUILD
COMMAND echo "\"generated_compile_flags.append('-D_GLIBCXX_USE_CXX11_ABI=0')\"" >> ${of_pyscript_dir}/oneflow/python_gen/sysconfig.py
)
endif()
add_dependencies(of_pyscript_copy of_protoobj)
add_custom_target(generate_api ALL
......
/*
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.
*/
#include "oneflow/api/python/of_api_registry.h"
namespace oneflow {
ONEFLOW_API_PYBIND11_MODULE("flags", m) {
m.def("with_cuda", []() {
#ifdef WITH_CUDA
return true;
#else
return false;
#endif // WITH_CUDA
});
m.def("use_cxx11_abi", []() {
#if _GLIBCXX_USE_CXX11_ABI == 1
return true;
#else
return false;
#endif // _GLIBCXX_USE_CXX11_ABI
});
}
} // namespace oneflow
......@@ -81,16 +81,14 @@ def api_gpu_device_num(val: int) -> None:
val (int): number of GPUs. It is identical on every machine. In other words,
you can't specify different number of GPUs you would like to use on each machine.
"""
from oneflow.python_gen.compatibility import with_cuda
if with_cuda == False:
if oneflow_api.flags.with_cuda():
return enable_if.unique([gpu_device_num, do_nothing])(val)
else:
print(
"INFO: for CPU-only OneFlow, oneflow.config.gpu_device_num is equivalent to oneflow.config.cpu_device_num"
)
print(traceback.format_stack()[-2])
return enable_if.unique([cpu_device_num, do_nothing])(val)
else:
return enable_if.unique([gpu_device_num, do_nothing])(val)
@enable_if.condition(hob.in_normal_mode & ~hob.session_initialized)
......
......@@ -23,6 +23,7 @@ from oneflow.python.oneflow_export import oneflow_export, oneflow_deprecate
import oneflow.python.lib.core.enable_if as enable_if
import oneflow
import traceback
import oneflow_api
@oneflow_export("device_prior_placement", "fixed_placement")
......@@ -54,18 +55,18 @@ def api_placement(
For example:
If you run program on single machine, you can assign the specified device like this:
If you run program on single machine, you can assign the specified device like this:
.. code-block:: python
.. code-block:: python
with flow.scope.placement("gpu", "0:0"):
logits = lenet(images, train=False)
loss = flow.nn.sparse_softmax_cross_entropy_with_logits(labels, logits, name="softmax_loss")
flow.losses.add_loss(loss)
Or you run distributed program, you can assign the specified devices like this:
Or you run distributed program, you can assign the specified devices like this:
.. code-block:: python
.. code-block:: python
# configure machines ids, ips, etc.
with flow.scope.placement("gpu", ['0:0-7', '1:0-7']):
......@@ -74,9 +75,8 @@ def api_placement(
flow.losses.add_loss(loss)
"""
from oneflow.python_gen.compatibility import with_cuda
if with_cuda == False:
if oneflow_api.flags.with_cuda() == False:
device_tag = "cpu"
func = enable_if.unique(
[
......
......@@ -489,11 +489,9 @@ def _TryCompleteConfigProto(config_proto):
def _GetDefaultConfigProto():
from oneflow.python_gen.compatibility import with_cuda
config_proto = job_set_util.ConfigProto()
config_proto.resource.machine_num = 0
if with_cuda:
if oneflow_api.flags.with_cuda():
config_proto.resource.gpu_device_num = 1
else:
config_proto.resource.cpu_device_num = 1
......
......@@ -21,8 +21,8 @@ import importlib.util
import oneflow
from oneflow.python.oneflow_export import oneflow_export
from oneflow.python_gen.sysconfig import generated_compile_flags
from typing import List
import oneflow_api
@oneflow_export("sysconfig.get_include")
......@@ -40,7 +40,12 @@ def get_compile_flags() -> List[str]:
flags = []
flags.append("-I{}".format(get_include()))
flags.append("-DHALF_ENABLE_CPP11_USER_LITERALS=0")
flags.extend(generated_compile_flags)
if oneflow_api.flags.with_cuda():
flags.append("-DWITH_CUDA")
if oneflow_api.flags.use_cxx11_abi():
flags.append("-D_GLIBCXX_USE_CXX11_ABI=1")
else:
flags.append("-D_GLIBCXX_USE_CXX11_ABI=0")
return flags
......
......@@ -101,8 +101,8 @@ def compare_with_tensorflow_grad(
input_maxval=10,
out_rtol=1e-5,
out_atol=1e-5,
diff_rtol=1e-5,
diff_atol=1e-5,
diff_rtol=1e-4,
diff_atol=1e-3,
):
assert device_type in ["gpu", "cpu"]
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment