forked from google-ml-infra/rules_ml_toolchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvidia_common_rules.bzl
More file actions
737 lines (680 loc) · 27.9 KB
/
Copy pathnvidia_common_rules.bzl
File metadata and controls
737 lines (680 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# Copyright 2025 The TensorFlow 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.
"""Common rules and functions for hermetic NVIDIA repositories."""
load("//common:tar_extraction_utils.bzl", "extract_tar_with_hermetic_tar_tool")
load("//common:repo.bzl", "tf_mirror_urls")
OS_ARCH_DICT = {
"amd64": "x86_64-unknown-linux-gnu",
"aarch64": "aarch64-unknown-linux-gnu",
"tegra-aarch64": "tegra-aarch64-unknown-linux-gnu",
}
_REDIST_ARCH_DICT = {
"linux-x86_64": "x86_64-unknown-linux-gnu",
"linux-sbsa": "aarch64-unknown-linux-gnu",
"linux-aarch64": "tegra-aarch64-unknown-linux-gnu",
}
_SUPPORTED_ARCHIVE_EXTENSIONS = [
".zip",
".jar",
".war",
".aar",
".tar",
".tar.gz",
".tgz",
".tar.xz",
".txz",
".tar.zst",
".tzst",
".tar.bz2",
".tbz",
".ar",
".deb",
".whl",
]
_TEGRA = "tegra"
_LIB_EXTENSION = ".so."
def get_env_var(repository_ctx, name):
return repository_ctx.getenv(name)
def get_cuda_version(repository_ctx):
return (get_env_var(repository_ctx, "HERMETIC_CUDA_VERSION") or
get_env_var(repository_ctx, "TF_CUDA_VERSION"))
def _get_file_name(url):
last_slash_index = url.rfind("/")
return url[last_slash_index + 1:]
def _get_orig_repo_name(repository_ctx):
"""Get the repo name used when this repository rule was called"""
if hasattr(repository_ctx, "original_name") and repository_ctx.original_name:
# For bazel 8 and above.
return repository_ctx.original_name
# With Bzlmod, the repo name will be something like `_main~cuda_redist_init_ext~cuda_nvml`,
# we need to extract the original repo name.
return repository_ctx.name.split("~")[-1]
def get_archive_name(url):
# buildifier: disable=function-docstring-return
# buildifier: disable=function-docstring-args
"""Returns the archive name without extension."""
filename = _get_file_name(url)
for extension in _SUPPORTED_ARCHIVE_EXTENSIONS:
if filename.endswith(extension):
return filename[:-len(extension)]
return filename
def _get_lib_name_and_version(path):
extension_index = path.rfind(_LIB_EXTENSION)
last_slash_index = path.rfind("/")
lib_name = path[last_slash_index + 1:extension_index]
lib_version = path[extension_index + len(_LIB_EXTENSION):]
return (lib_name, lib_version)
def _get_main_lib_name(repository_ctx):
if _get_orig_repo_name(repository_ctx) == "cuda_driver":
return "libcuda"
if _get_orig_repo_name(repository_ctx) == "cuda_nvml":
return "libnvidia-ml"
if _get_orig_repo_name(repository_ctx) == "nvidia_nvshmem":
return "libnvshmem_host"
else:
return "lib{}".format(
_get_common_lib_name(repository_ctx),
)
def _get_common_lib_name(repository_ctx):
return _get_orig_repo_name(repository_ctx).split("_")[1].lower()
def _get_libraries_by_redist_name_in_dir(repository_ctx):
lib_dir_path = repository_ctx.path("lib")
if not lib_dir_path.exists:
return []
common_lib_name = _get_common_lib_name(repository_ctx)
lib_dir_content = lib_dir_path.readdir()
return [
str(f)
for f in lib_dir_content
if (_LIB_EXTENSION in str(f) and
common_lib_name in str(f).lower())
]
def get_lib_name_to_version_dict(repository_ctx):
# buildifier: disable=function-docstring-return
# buildifier: disable=function-docstring-args
"""Returns a dict of library names and major versions."""
lib_name_to_version_dict = {}
for path in _get_libraries_by_redist_name_in_dir(repository_ctx):
lib_name, lib_version = _get_lib_name_and_version(path)
major_version_key = "%%{%s_version}" % lib_name.lower()
minor_version_key = "%%{%s_minor_version}" % lib_name.lower()
# We need to find either major or major.minor version if there is no
# file with major version. E.g. if we have the following files:
# libcudart.so
# libcudart.so.12
# libcudart.so.12.3.2,
# we will save save {"%{libcudart_version}": "12",
# "%{libcudart_minor_version}": "12.3.2"}
if len(lib_version.split(".")) == 1:
lib_name_to_version_dict[major_version_key] = lib_version
if len(lib_version.split(".")) == 2:
lib_name_to_version_dict[minor_version_key] = lib_version
if (major_version_key not in lib_name_to_version_dict or
len(lib_name_to_version_dict[major_version_key].split(".")) > 2):
lib_name_to_version_dict[major_version_key] = lib_version
if len(lib_version.split(".")) >= 3:
if major_version_key not in lib_name_to_version_dict:
lib_name_to_version_dict[major_version_key] = lib_version
if minor_version_key not in lib_name_to_version_dict:
lib_name_to_version_dict[minor_version_key] = lib_version
return lib_name_to_version_dict
def create_dummy_build_file(repository_ctx, cuda_version, is_local_redist, use_comment_symbols = True):
if is_local_redist:
build_template = repository_ctx.attr.local_build_templates[0]
else:
build_template = repository_ctx.attr.build_templates[0]
repository_ctx.template(
"BUILD",
build_template,
{
"%{multiline_comment}": "'''" if use_comment_symbols else "",
"%{comment}": "#" if use_comment_symbols else "",
"%{version_of_cuda}": cuda_version or "",
},
)
def _get_build_template(repository_ctx, major_lib_version, is_local_redist):
template = None
for i in range(0, len(repository_ctx.attr.versions)):
for dist_version in repository_ctx.attr.versions[i].split(","):
if dist_version == major_lib_version:
if is_local_redist:
template = repository_ctx.attr.local_build_templates[i]
else:
template = repository_ctx.attr.build_templates[i]
break
if not template:
fail("No build template found for {} version {}".format(
_get_orig_repo_name(repository_ctx),
major_lib_version,
))
return template
def get_major_library_version(repository_ctx, lib_name_to_version_dict):
# buildifier: disable=function-docstring-return
# buildifier: disable=function-docstring-args
"""Returns the major library version provided the versions dict."""
main_lib_name = _get_main_lib_name(repository_ctx)
key = "%%{%s_version}" % main_lib_name
if key not in lib_name_to_version_dict:
return ""
return lib_name_to_version_dict[key]
def create_build_file(
repository_ctx,
cuda_version,
lib_name_to_version_dict,
major_lib_version,
is_local_redist):
# buildifier: disable=function-docstring-args
"""Creates a BUILD file for the repository."""
if len(major_lib_version) == 0:
if is_local_redist:
build_template = repository_ctx.attr.local_build_templates[0]
else:
build_template = repository_ctx.attr.build_templates[0]
build_template_content = repository_ctx.read(build_template)
create_dummy_build_file(
repository_ctx,
cuda_version,
is_local_redist = is_local_redist,
use_comment_symbols = True if "_version}" in build_template_content else False,
)
return
build_template = _get_build_template(
repository_ctx,
major_lib_version.split(".")[0],
is_local_redist,
)
repository_ctx.template(
"BUILD",
build_template,
lib_name_to_version_dict | {
"%{multiline_comment}": "",
"%{comment}": "",
},
)
def _create_symlinks(repository_ctx, local_path, dirs):
for dir in dirs:
dir_path = "{path}/{dir}".format(
path = local_path,
dir = dir,
)
if not repository_ctx.path(local_path).exists:
fail("%s directory doesn't exist!" % dir_path)
repository_ctx.symlink(dir_path, dir)
def _create_libcuda_symlinks(
repository_ctx,
lib_name_to_version_dict):
lib_names = ["cuda", "nvidia-ml", "nvidia-ptxjitcompiler"]
if _get_orig_repo_name(repository_ctx) == "cuda_driver":
for lib in lib_names:
key = "%" + "{lib%s_version}" % lib
if key not in lib_name_to_version_dict:
return
versioned_lib_path = "lib/lib{}.so.{}".format(
lib,
lib_name_to_version_dict[key],
)
if not repository_ctx.path(versioned_lib_path).exists:
fail("%s doesn't exist!" % versioned_lib_path)
symlink_so_1 = "lib/lib%s.so.1" % lib
if repository_ctx.path(symlink_so_1).exists:
print("File %s already exists!" % repository_ctx.path(symlink_so_1)) # buildifier: disable=print
else:
repository_ctx.symlink(versioned_lib_path, symlink_so_1)
if lib == "cuda":
unversioned_symlink = "lib/lib%s.so" % lib
if repository_ctx.path(unversioned_symlink).exists:
print("File %s already exists!" % repository_ctx.path(unversioned_symlink)) # buildifier: disable=print
else:
repository_ctx.symlink(symlink_so_1, unversioned_symlink)
def _create_repository_symlinks(repository_ctx):
for target, link_name in repository_ctx.attr.repository_symlinks.items():
target_path = repository_ctx.path(target)
if not target_path.exists:
print("Target %s doesn't exist!" % target_path) # buildifier: disable=print
continue
if repository_ctx.path(link_name).exists:
print("File %s already exists!" % repository_ctx.path(link_name)) # buildifier: disable=print
continue
repository_ctx.symlink(target_path, link_name)
def create_version_file(repository_ctx, major_lib_version):
if repository_ctx.name == "cuda_driver" and major_lib_version:
print("Downloaded hermetic User Mode Driver version is %s" % major_lib_version) # buildifier: disable=print
repository_ctx.file(
"version.bzl",
"VERSION = \"{}\"".format(major_lib_version),
)
def use_local_redist_path(repository_ctx, local_redist_path, dirs):
# buildifier: disable=function-docstring-args
"""Creates repository using local redistribution paths."""
_create_symlinks(
repository_ctx,
local_redist_path,
dirs,
)
lib_name_to_version_dict = get_lib_name_to_version_dict(repository_ctx)
major_version = get_major_library_version(
repository_ctx,
lib_name_to_version_dict,
)
create_build_file(
repository_ctx,
get_cuda_version(repository_ctx),
lib_name_to_version_dict,
major_version,
is_local_redist = True,
)
_create_libcuda_symlinks(
repository_ctx,
lib_name_to_version_dict,
)
create_version_file(repository_ctx, major_version)
def _download_redistribution(
repository_ctx,
arch_key,
path_prefix,
mirrored_tar_path_prefix):
# buildifier: disable=function-docstring-args
"""Downloads and extracts NVIDIA redistribution."""
(url, sha256, custom_strip_prefix) = repository_ctx.attr.url_dict[arch_key]
# If url is not relative, then appending prefix is not needed.
if not (url.startswith("http") or url.startswith("file:///")):
if url.endswith(".tar"):
url = mirrored_tar_path_prefix + url
else:
url = path_prefix + url
archive_name = get_archive_name(url)
file_name = _get_file_name(url)
urls = [url] if url.endswith(".tar") else tf_mirror_urls(url)
print("Downloading and extracting {}".format(url)) # buildifier: disable=print
repository_ctx.download(
url = urls,
output = file_name,
sha256 = sha256,
)
if custom_strip_prefix:
strip_prefix = custom_strip_prefix
else:
strip_prefix = archive_name
if url.endswith(".tar.xz") or url.endswith(".tar"):
extract_tar_with_hermetic_tar_tool(repository_ctx, file_name, strip_prefix)
else:
repository_ctx.extract(
archive = file_name,
stripPrefix = strip_prefix,
)
repository_ctx.delete(file_name)
def _get_platform_architecture(repository_ctx):
# buildifier: disable=function-docstring-return
# buildifier: disable=function-docstring-args
"""Returns the platform architecture for the redistribution."""
target_arch = get_env_var(repository_ctx, repository_ctx.attr.target_arch_env_var)
# We use NVCC compiler as the host compiler.
if target_arch and _get_orig_repo_name(repository_ctx) != "cuda_nvcc":
if target_arch in OS_ARCH_DICT.keys():
host_arch = target_arch
else:
fail(
"Unsupported architecture: {arch}, use one of {supported}".format(
arch = target_arch,
supported = OS_ARCH_DICT.keys(),
),
)
else:
host_arch = repository_ctx.os.arch
if host_arch == "aarch64":
uname_result = repository_ctx.execute(["uname", "-a"]).stdout
if _TEGRA in uname_result:
return "{}-{}".format(_TEGRA, host_arch)
return host_arch
def _use_downloaded_redistribution(repository_ctx):
# buildifier: disable=function-docstring-args
""" Downloads redistribution and initializes hermetic repository."""
major_version = ""
redist_version = _get_redist_version(
repository_ctx,
repository_ctx.attr.redist_version_env_vars,
)
cuda_version = get_cuda_version(repository_ctx)
if not redist_version:
# If no toolkit version is found, comment out cc_import targets.
create_dummy_build_file(repository_ctx, cuda_version, is_local_redist = False)
create_version_file(repository_ctx, major_version)
return
if len(repository_ctx.attr.url_dict) == 0:
print("{} is not found in redistributions list.".format(
_get_orig_repo_name(repository_ctx),
)) # buildifier: disable=print
create_dummy_build_file(repository_ctx, cuda_version, is_local_redist = False)
create_version_file(repository_ctx, major_version)
return
# Download archive only when GPU config is used.
arch_key = OS_ARCH_DICT[_get_platform_architecture(repository_ctx)]
if arch_key not in repository_ctx.attr.url_dict.keys():
arch_key = "cuda{version}_{arch}".format(
version = cuda_version.split(".")[0],
arch = arch_key,
)
if arch_key not in repository_ctx.attr.url_dict.keys():
fail(
("{dist_name}: The supported platforms are {supported_platforms}." +
" Platform {platform} is not supported.")
.format(
supported_platforms = repository_ctx.attr.url_dict.keys(),
platform = arch_key,
dist_name = _get_orig_repo_name(repository_ctx),
),
)
_download_redistribution(
repository_ctx,
arch_key,
repository_ctx.attr.redist_path_prefix,
repository_ctx.attr.mirrored_tar_redist_path_prefix,
)
lib_name_to_version_dict = get_lib_name_to_version_dict(repository_ctx)
major_version = get_major_library_version(
repository_ctx,
lib_name_to_version_dict,
)
create_build_file(
repository_ctx,
cuda_version,
lib_name_to_version_dict,
major_version,
is_local_redist = False,
)
_create_libcuda_symlinks(
repository_ctx,
lib_name_to_version_dict,
)
_create_repository_symlinks(repository_ctx)
create_version_file(repository_ctx, major_version)
def _redist_repo_impl(repository_ctx):
local_redist_path = get_env_var(repository_ctx, repository_ctx.attr.local_path_env_var)
if local_redist_path:
use_local_redist_path(repository_ctx, local_redist_path, repository_ctx.attr.local_source_dirs)
else:
_use_downloaded_redistribution(repository_ctx)
_redist_repo = repository_rule(
implementation = _redist_repo_impl,
attrs = {
"url_dict": attr.string_list_dict(mandatory = True),
"versions": attr.string_list(mandatory = True),
"build_templates": attr.label_list(mandatory = True),
"local_build_templates": attr.label_list(mandatory = True),
"redist_path_prefix": attr.string(),
"mirrored_tar_redist_path_prefix": attr.string(mandatory = False),
"redist_version_env_vars": attr.string_list(mandatory = True),
"local_path_env_var": attr.string(mandatory = True),
"use_tar_file_env_var": attr.string(mandatory = True),
"target_arch_env_var": attr.string(mandatory = True),
"local_source_dirs": attr.string_list(mandatory = False),
"repository_symlinks": attr.label_keyed_string_dict(mandatory = False),
"xz_tool": attr.label(
default = Label("@xz//:bin/xz"),
allow_single_file = True,
),
"tar_tool": attr.label(
default = Label("@tar//:bin/tar"),
allow_single_file = True,
),
},
environ = ["HERMETIC_CUDA_VERSION", "TF_CUDA_VERSION"],
)
def redist_init_repository(
name,
versions,
build_templates,
local_build_templates,
url_dict,
redist_path_prefix,
mirrored_tar_redist_path_prefix,
redist_version_env_vars,
local_path_env_var,
use_tar_file_env_var,
target_arch_env_var,
local_source_dirs,
repository_symlinks = {}):
# buildifier: disable=function-docstring-args
"""Initializes repository for individual NVIDIA redistribution."""
_redist_repo(
name = name,
url_dict = url_dict,
versions = versions,
build_templates = build_templates,
local_build_templates = local_build_templates,
redist_path_prefix = redist_path_prefix,
mirrored_tar_redist_path_prefix = mirrored_tar_redist_path_prefix,
redist_version_env_vars = redist_version_env_vars,
local_path_env_var = local_path_env_var,
use_tar_file_env_var = use_tar_file_env_var,
target_arch_env_var = target_arch_env_var,
local_source_dirs = local_source_dirs,
repository_symlinks = repository_symlinks,
)
def get_redistribution_urls(dist_info):
# buildifier: disable=function-docstring-return
# buildifier: disable=function-docstring-args
"""Returns a dict of redistribution URLs and their SHA256 values."""
url_dict = {}
for arch in _REDIST_ARCH_DICT.keys():
arch_key = arch
if arch_key == "linux-aarch64" and arch_key not in dist_info:
arch_key = "linux-sbsa"
if arch_key not in dist_info:
continue
if "relative_path" in dist_info[arch_key]:
url_dict[_REDIST_ARCH_DICT[arch]] = [
dist_info[arch_key]["relative_path"],
dist_info[arch_key].get("sha256", ""),
dist_info[arch_key].get("strip_prefix", ""),
]
continue
if "full_path" in dist_info[arch_key]:
url_dict[_REDIST_ARCH_DICT[arch]] = [
dist_info[arch_key]["full_path"],
dist_info[arch_key].get("sha256", ""),
dist_info[arch_key].get("strip_prefix", ""),
]
continue
for cuda_version, data in dist_info[arch_key].items():
# CUDNN and NVSHMEM JSON might contain paths for each CUDA version.
path_key = "relative_path"
if path_key not in data.keys():
path_key = "full_path"
url_dict["{cuda_version}_{arch}".format(
cuda_version = cuda_version,
arch = _REDIST_ARCH_DICT[arch],
)] = [data[path_key], data.get("sha256", ""), data.get("strip_prefix", "")]
return url_dict
def get_version_and_template_lists(version_to_template):
# buildifier: disable=function-docstring-return
# buildifier: disable=function-docstring-args
"""Returns lists of versions and templates provided in the dict."""
template_to_version_map = {}
for version, template in version_to_template.items():
if template not in template_to_version_map.keys():
template_to_version_map[template] = [version]
else:
template_to_version_map[template].append(version)
version_list = []
template_list = []
for template, versions in template_to_version_map.items():
version_list.append(",".join(versions))
template_list.append(Label(template))
return (version_list, template_list)
def get_local_templates(local_repo_data, templates):
if "version_to_template" in local_repo_data:
_, local_templates = get_version_and_template_lists(
local_repo_data["version_to_template"],
)
else:
local_templates = templates
return local_templates
def _get_json_file_content(
repository_ctx,
url_to_sha256,
mirrored_tars_url_to_sha256,
json_file_name,
mirrored_tars_json_file_name,
use_tar_file_env_var):
# buildifier: disable=function-docstring-args
# buildifier: disable=function-docstring-return
""" Returns the JSON file content for the NVIDIA redistributions."""
use_tars = get_env_var(
repository_ctx,
use_tar_file_env_var,
)
(url, sha256) = url_to_sha256
if mirrored_tars_url_to_sha256:
(mirrored_tar_url, mirrored_tar_sha256) = mirrored_tars_url_to_sha256
else:
mirrored_tar_url = None
mirrored_tar_sha256 = None
json_file = None
if use_tars and mirrored_tar_url:
json_tar_downloaded = repository_ctx.download(
url = mirrored_tar_url,
sha256 = mirrored_tar_sha256,
output = mirrored_tars_json_file_name,
allow_fail = True,
)
if json_tar_downloaded.success:
print("Successfully downloaded mirrored tar file: {}".format(
mirrored_tar_url,
)) # buildifier: disable=print
json_file = mirrored_tars_json_file_name
else:
print("Failed to download mirrored tar file: {}".format(
mirrored_tar_url,
)) # buildifier: disable=print
if not json_file:
repository_ctx.download(
url = tf_mirror_urls(url),
sha256 = sha256,
output = json_file_name,
)
json_file = json_file_name
json_content = repository_ctx.read(repository_ctx.path(json_file))
repository_ctx.delete(json_file)
return json_content
def _get_redist_version(repository_ctx, redist_version_env_vars):
redist_version = None
for redist_version_env_var in redist_version_env_vars:
redist_version = get_env_var(repository_ctx, redist_version_env_var)
if redist_version:
if repository_ctx.name == "cuda_umd_redist_json":
print("User Mode Driver for CUDA {} will be downloaded according to the value of {}".format(
redist_version,
redist_version_env_var,
)) # buildifier: disable=print
break
return redist_version
def _redist_json_impl(repository_ctx):
redist_version = _get_redist_version(
repository_ctx,
repository_ctx.attr.redist_version_env_vars,
)
local_redist_path = get_env_var(repository_ctx, repository_ctx.attr.local_path_env_var)
supported_redist_versions = repository_ctx.attr.json_dict.keys()
if (redist_version and not local_redist_path and
(redist_version not in supported_redist_versions)):
fail(
("The supported {toolkit_name} versions are {supported_versions}." +
" Please provide a supported version in {redist_version_env_vars}" +
" environment variable(s) or add JSON URL for" +
" {toolkit_name} version={version}.")
.format(
toolkit_name = repository_ctx.attr.toolkit_name,
redist_version_env_vars = repository_ctx.attr.redist_version_env_vars,
supported_versions = supported_redist_versions,
version = redist_version,
),
)
redistributions = "{}"
if redist_version and not local_redist_path:
if redist_version in repository_ctx.attr.mirrored_tars_json_dict.keys():
mirrored_tars_url_to_sha256 = repository_ctx.attr.mirrored_tars_json_dict[redist_version]
else:
mirrored_tars_url_to_sha256 = {}
redistributions = _get_json_file_content(
repository_ctx,
url_to_sha256 = repository_ctx.attr.json_dict[redist_version],
mirrored_tars_url_to_sha256 = mirrored_tars_url_to_sha256,
json_file_name = "redistrib_{toolkit_name}_{redist_version}.json".format(
toolkit_name = repository_ctx.attr.toolkit_name,
redist_version = redist_version,
),
mirrored_tars_json_file_name = "redistrib_{toolkit_name}_{redist_version}_tar.json".format(
toolkit_name = repository_ctx.attr.toolkit_name,
redist_version = redist_version,
),
use_tar_file_env_var = repository_ctx.attr.use_tar_file_env_var,
)
repository_ctx.file(
"distributions.bzl",
"{toolkit_name}_REDISTRIBUTIONS = {redistributions}".format(
toolkit_name = repository_ctx.attr.toolkit_name,
redistributions = redistributions,
),
)
repository_ctx.file(
"BUILD",
"",
)
_redist_json = repository_rule(
implementation = _redist_json_impl,
attrs = {
"toolkit_name": attr.string(mandatory = True),
"json_dict": attr.string_list_dict(mandatory = True),
"mirrored_tars_json_dict": attr.string_list_dict(mandatory = True),
"redist_version_env_vars": attr.string_list(mandatory = True),
"local_path_env_var": attr.string(mandatory = True),
"use_tar_file_env_var": attr.string(mandatory = True),
},
)
def json_init_repository(
name,
toolkit_name,
json_dict,
mirrored_tars_json_dict,
redist_version_env_vars,
local_path_env_var,
use_tar_file_env_var):
# buildifier: disable=function-docstring-args
"""Initializes NVIDIA redistribution JSON repository."""
_redist_json(
name = name,
toolkit_name = toolkit_name,
json_dict = json_dict,
mirrored_tars_json_dict = mirrored_tars_json_dict,
redist_version_env_vars = redist_version_env_vars,
local_path_env_var = local_path_env_var,
use_tar_file_env_var = use_tar_file_env_var,
)
# Constructs rpath linker flags for use with nvidia wheel-packaged libs
# avaialble from PyPI. Two paths are needed because symbols are used from
# both the root of the TensorFlow installation directory as well as from
# various pywrap libs within the 'python' subdir.
def cuda_rpath_flags(relpath):
return select({
"@rules_ml_toolchain//common:enable_cuda_rpath": [
"-Wl,-rpath='$$ORIGIN/../../" + relpath + "'",
"-Wl,-rpath='$$ORIGIN/../" + relpath + "'",
],
"//conditions:default": [],
})
def cuda_lib_header_prefix(major_version, wanted_major_version, new_header_prefix, old_header_prefix):
if not major_version:
return old_header_prefix
return new_header_prefix if int(major_version) >= wanted_major_version else old_header_prefix