easyblock = 'PythonBundle'

name = 'TensorFlow'
version = '2.11.0'
versionsuffix = '-CUDA-%(cudaver)s'

homepage = 'https://www.tensorflow.org/'
description = "An open-source software library for Machine Intelligence"

toolchain = {'name': 'foss', 'version': '2022a'}
toolchainopts = {'pic': True}

builddependencies = [
    ('Bazel', '5.1.1'),
    ('protobuf', '3.19.4'),
    # git 2.x required, see also https://github.com/tensorflow/tensorflow/issues/29053
    ('git', '2.36.0', '-nodocs'),
    ('pybind11', '2.9.2'),
    ('UnZip', '6.0'),
    ('LLVM', '14.0.3'),  # for debugging with llvm-symbolizer, to be removed
]
dependencies = [
    ('CUDA', '11.7', '', SYSTEM),
    ('cuDNN', '8.6.0.163', versionsuffix, SYSTEM),
    ('NCCL', 'default', versionsuffix),
    ('Python', '3.10.4'),
    ('h5py', '3.7.0'),
    ('cURL', '7.83.0'),
    ('dill', '0.3.6'),
    ('double-conversion', '3.2.0'),
    ('flatbuffers', '2.0.7'),
    ('giflib', '5.2.1'),
    ('hwloc', '2.7.1'),
    ('ICU', '71.1'),
    ('JsonCpp', '1.9.5'),
    ('libjpeg-turbo', '2.1.3'),
    ('LMDB', '0.9.29'),
    ('NASM', '2.15.05'),
    ('nsync', '1.25.0'),
    ('SQLite', '3.38.3'),
    ('protobuf-python', '3.19.4'),
    ('libpng', '1.6.37'),
    ('snappy', '1.1.9'),
    ('zlib', '1.2.12'),
    ('networkx', '2.8.4', '', ('gcccoremkl', '11.3.0-2022.1.0')),  # required for pythran
]

use_pip = True
sanity_pip_check = True

# Dependencies created and updated using findPythonDeps.sh:
# https://gist.github.com/Flamefire/49426e502cd8983757bd01a08a10ae0d
exts_list = [
    ('wrapt', '1.14.1', {
        'checksums': ['380a85cf89e0e69b7cfbe2ea9f765f004ff419f34194018a6827ac0e3edfed4d'],
    }),
    ('termcolor', '1.1.0', {
        'checksums': ['1d6d69ce66211143803fbc56652b41d73b4a400a2891d7bf7a1cdf4c02de613b'],
    }),
    ('tensorflow-estimator', version, {
        'source_tmpl': 'tensorflow_estimator-%(version)s-py2.py3-none-any.whl',
        'checksums': ['ea3b64acfff3d9a244f06178c9bdedcbdd3f125b67d0888dba8229498d06468b'],
    }),
    ('Werkzeug', '2.2.2', {
        'checksums': ['7ea2d48322cc7c0f8b3a215ed73eabd7b5d75d0b50e31ab006286ccff9e00b8f'],
    }),
    ('tensorboard-plugin-wit', '1.8.1', {
        'source_tmpl': 'tensorboard_plugin_wit-%(version)s-py3-none-any.whl',
        'checksums': ['ff26bdd583d155aa951ee3b152b3d0cffae8005dc697f72b44a8e8c2a77a8cbe'],
    }),
    ('tensorboard-data-server', '0.6.1', {
        'source_tmpl': 'tensorboard_data_server-%(version)s-py3-none-any.whl',
        'checksums': ['809fe9887682d35c1f7d1f54f0f40f98bb1f771b14265b453ca051e2ce58fca7'],
    }),
    ('Markdown', '3.4.1', {
        'checksums': ['3b809086bb6efad416156e00a0da66fe47618a5d6918dd688f53f40c8e4cfeff'],
    }),
    ('grpcio', '1.51.1', {
        'modulename': 'grpc',
        'preinstallopts': "export GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS=%(parallel)s && ",
        'checksums': ['e6dfc2b6567b1c261739b43d9c59d201c1b89e017afd9e684d85aa7a186c9f7a'],
    }),
    ('oauthlib', '3.2.2', {
        'checksums': ['9859c40929662bec5d64f34d01c99e093149682a3f38915dc0655d5a633dd918'],
    }),
    ('requests-oauthlib', '1.3.1', {
        'checksums': ['75beac4a47881eeb94d5ea5d6ad31ef88856affe2332b9aafb52c6452ccf0d7a'],
    }),
    ('rsa', '4.9', {
        'checksums': ['e38464a49c6c85d7f1351b0126661487a7e0a14a50f1675ec50eb34d4f20ef21'],
    }),
    ('pyasn1-modules', '0.2.8', {
        'checksums': ['905f84c712230b2c592c19470d3ca8d552de726050d1d1716282a1f6146be65e'],
    }),
    ('cachetools', '5.2.1', {
        'checksums': ['5991bc0e08a1319bb618d3195ca5b6bc76646a49c21d55962977197b301cc1fe'],
    }),
    ('google-auth', '2.16.0', {
        'modulename': 'google.auth',
        'checksums': ['ed7057a101af1146f0554a769930ac9de506aeca4fd5af6543ebe791851a9fbd'],
    }),
    ('google-auth-oauthlib', '0.4.6', {
        'checksums': ['a90a072f6993f2c327067bf65270046384cda5a8ecb20b94ea9a687f1f233a7a'],
    }),
    ('absl-py', '1.4.0', {
        'modulename': 'absl',
        'checksums': ['d2c244d01048ba476e7c080bd2c6df5e141d211de80223460d5b3b8a2a58433d'],
    }),
    ('tensorboard', '2.11.1', {
        'source_tmpl': '%(name)s-%(version)s-py3-none-any.whl',
        'checksums': ['0c7529f3f43691e8cc2ece8e564c2e103c51ade317c6af626d415239b5088018'],
    }),
    ('opt-einsum', '3.3.0', {
        'source_tmpl': 'opt_einsum-%(version)s.tar.gz',
        'checksums': ['59f6475f77bbc37dcf7cd748519c0ec60722e91e63ca114e68821c0c54a46549'],
    }),
    ('keras', version, {
        'source_tmpl': '%(name)s-%(version)s-py2.py3-none-any.whl',
        'checksums': ['38c6fff0ea9a8b06a2717736565c92a73c8cd9b1c239e7125ccb188b7848f65e'],
    }),
    ('google-pasta', '0.2.0', {
        'modulename': 'pasta',
        'checksums': ['c9f2c8dfc8f96d0d5808299920721be30c9eec37f2389f28904f454565c8a16e'],
    }),
    ('astunparse', '1.6.3', {
        'checksums': ['5ad93a8456f0d084c3456d059fd9a92cce667963232cbf763eac3bc5b7940872'],
    }),
    ('gast', '0.4.0', {
        'checksums': ['40feb7b8b8434785585ab224d1568b857edb18297e5a3047f1ba012bc83b42c1'],
    }),
    ('beniget', '0.3.0', {
        'checksums': ['062c893be9cdf87c3144fb15041cce4d81c67107c1591952cd45fdce789a0ff1'],
    }),
    ('pythran', '0.9.11', {
        'checksums': ['a317f91e2aade9f6550dc3bf40b5caeb45b7e012daf27e2b3e4ad928edb01667'],
    }),
    ('portpicker', '1.5.2', {
        'checksums': ['c55683ad725f5c00a41bc7db0225223e8be024b1fa564d039ed3390e4fd48fb3'],
    }),
    ('tblib', '1.7.0', {
        'checksums': ['059bd77306ea7b419d4f76016aef6d7027cc8a0785579b5aad198803435f882c'],
    }),
    ('astor', '0.8.1', {
        'checksums': ['6a6effda93f4e1ce9f618779b2dd1d9d84f1e32812c23a29b3fff6fd7f63fa5e'],
    }),
    ('gviz-api', '1.10.0', {
        'source_tmpl': 'gviz_api-%(version)s.tar.gz',
        'checksums': ['846692dd8cc73224fc31b18e41589bd934e1cc05090c6576af4b4b26c2e71b90'],
    }),
    ('tensorboard-plugin-profile', '2.11.1', {
        'source_tmpl': 'tensorboard_plugin_profile-%(version)s.tar.gz',
        'checksums': ['7a97c02d502fde98a0336aadcab15b37169744d5bf6966af7d576c7a3f0a89f4'],
    }),
    (name, version, {
        'patches': [
            '%(name)s-2.4.0_dont-use-var-lock.patch',
            '%(name)s-2.5.0-fix-alias-violation-in-absl.patch',
            '%(name)s-2.5.0_fix-crash-on-shutdown.patch',
            '%(name)s-2.8.4_exclude-xnnpack-on-ppc.patch',
            '%(name)s-2.8.4_resolve-gcc-symlinks.patch',
            '%(name)s-2.9.1_remove-duplicate-gpu-tests.patch',
            '%(name)s-%(version)s_disable-avx512-extensions.patch',
            '%(name)s-%(version)s_fix-eigen-atan-on-PPC.patch',
            '%(name)s-%(version)s_fix-eigen-gemm-on-PPC.patch',
            '%(name)s-%(version)s_fix-link-error.patch',
            '%(name)s-%(version)s_remove-libclang-and-io-gcs-deps.patch',
            ('tensorboard-2.11.1-jupyter.fix', 'tools'),
        ],
        'source_tmpl': 'v%(version)s.tar.gz',
        'source_urls': ['https://github.com/tensorflow/tensorflow/archive/'],
        'test_script': 'TensorFlow-2.x_mnist-test.py',
        'test_tag_filters_cpu': '-gpu,-tpu,-no_cuda_on_cpu_tap,-no_pip,-no_oss,-oss_serial,-benchmark-test,-v1only',
        'test_tag_filters_gpu': "gpu,-no_gpu,-nogpu,-gpu_cupti, \
                                -no_cuda11,-no_pip,-no_oss,-oss_serial,-benchmark-test,-v1only",
        'test_targets': [
            '//tensorflow/core/...',
            '-//tensorflow/core:example_java_proto',
            '-//tensorflow/core/example:example_protos_closure',
            '//tensorflow/cc/...',
            '//tensorflow/c/...',
            '//tensorflow/python/...',
            '-//tensorflow/c/eager:c_api_test_gpu',
            '-//tensorflow/c/eager:c_api_distributed_test',
            '-//tensorflow/c/eager:c_api_distributed_test_gpu',
            '-//tensorflow/c/eager:c_api_cluster_test_gpu',
            '-//tensorflow/c/eager:c_api_remote_function_test_gpu',
            '-//tensorflow/c/eager:c_api_remote_test_gpu',
            '-//tensorflow/core/common_runtime:collective_param_resolver_local_test',
            '-//tensorflow/core/common_runtime:mkl_layout_pass_test',
            '-//tensorflow/core/kernels/mkl:mkl_fused_ops_test',
            '-//tensorflow/core/kernels/mkl:mkl_fused_batch_norm_op_test',
            '-//tensorflow/core/ir/importexport/tests/roundtrip/...',
        ],
        'testopts': "--test_timeout=3600 --test_size_filters=small",
        'testopts_gpu': "--test_timeout=3600 --test_size_filters=small \
                         --run_under=//tensorflow/tools/ci_build/gpu_build:parallel_gpu_execute",
        'with_xla': True,
        'checksums': [
            {'v2.11.0.tar.gz': '99c732b92b1b37fc243a559e02f9aef5671771e272758aa4aec7f34dc92dac48'},
            {'TensorFlow-2.4.0_dont-use-var-lock.patch':
             'b14f2493fd2edf79abd1c4f2dde6c98a3e7d5cb9c25ab9386df874d5f072d6b5'},
            {'TensorFlow-2.5.0-fix-alias-violation-in-absl.patch':
             '12454fda3330fb45cd380377e283f04488b40e0b8ae7378e786ddf731a581f75'},
            {'TensorFlow-2.5.0_fix-crash-on-shutdown.patch':
             '578c7493221ebd3dc25ca43d63a72cbb28fdf4112b1e2baa7390f25781bd78fd'},
            {'TensorFlow-2.8.4_exclude-xnnpack-on-ppc.patch':
             'ebd404ac56cc4ca662483f1f591e62e11749361be57a7ba5f4b2f0d03e829884'},
            {'TensorFlow-2.8.4_resolve-gcc-symlinks.patch':
             '43ce9acc6bffff68a31d2263d0064d272999b2e0a2c6546690287cd1c9c90f04'},
            {'TensorFlow-2.9.1_remove-duplicate-gpu-tests.patch':
             '6fe50faab28387c622c68dc3fc0cbfb2a51000cd750c1a82f8420b54fcd2509f'},
            {'TensorFlow-2.11.0_disable-avx512-extensions.patch':
             'fb8e7694b5d2377cc44e6674ff85a7c50dc725f2f507cbcfda65f129f534b1cc'},
            {'TensorFlow-2.11.0_fix-eigen-atan-on-PPC.patch':
             'd9f4779f72ffd2c5f9c5da0a7735328dd25756515edccf603087dba2bf4d1612'},
            {'TensorFlow-2.11.0_fix-eigen-gemm-on-PPC.patch':
             '4f18ff0563b0ef8556904db1bb4974f8068bf0d2ee1effb24e0c779eb32517e3'},
            {'TensorFlow-2.11.0_fix-link-error.patch':
             '0a2f5c9c5be425f305bdc08f5a5ffce210e66f6ad4120d94ea0209246fc0449f'},
            {'TensorFlow-2.11.0_remove-libclang-and-io-gcs-deps.patch':
             '21d5723ab4e9a3be349cf3d57bb55cbe43e28785a8c8b19c52cee9dcc4dcf0de'},
            {'tensorboard-2.11.1-jupyter.fix':
             '8a1c3bf40880f8522bcfe6849604bb2084e8c7a4526c035f617795138a405ff7'},
        ],
    }),
]

postinstallcmds = [
    'patch -p0 -d %(installdir)s < %(builddir)s/TensorFlow/tensorflow-%(version)s/tools/tensorboard-2.11.1-jupyter.fix']

moduleclass = 'lib'