lib/accy/src/kernel/library/random/philox/artifact.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const random = @import("../root.zig");
2
3 const base = random.base;
4 const block = random.block;
5 const model = @import("model.zig");
6 const runtime = @import("runtime.zig");
7
8 const std = base.std;
9 const artifact_product = base.artifact_product;
10 const entry = base.entry;
11 const kernel = base.kernel;
12
13 const Philox = model.Philox;
14 const PhiloxRuntimeFamilyF32 = runtime.PhiloxRuntimeFamilyF32;
15 const PhiloxRuntimeFamilyI32 = runtime.PhiloxRuntimeFamilyI32;
16 const philox_lanes = block.philox_lanes;
17 const philox_family_version = model.philox_family_version;
18 const philoxFamilyTarget = model.philoxFamilyTarget;
19 const philoxFamilyEntryName = model.philoxFamilyEntryName;
20 const philoxFamilyFingerprint = model.philoxFamilyFingerprint;
21 const philoxShapeProfileDimensions = model.philoxShapeProfileDimensions;
22 const randomDerivedLaunch = base.randomDerivedLaunch;
23
24 pub fn createPhiloxFamilyArtifact(
25 allocator: std.mem.Allocator,
26 handle: kernel.BackendHandle,
27 instance: Philox,
28 options: entry.ArtifactOptions,
29 ) !kernel.OwnedKernelCallArtifact {
30 const target = try philoxFamilyTarget(allocator, instance);
31 defer allocator.free(target);
32 const entry_name = try philoxFamilyEntryName(allocator, instance);
33 defer allocator.free(entry_name);
34 const family_fingerprint = options.shape_family_fingerprint orelse try philoxFamilyFingerprint(allocator, instance);
35 const shape_profile_dimensions = philoxShapeProfileDimensions(instance);
36 const shape_profile = options.shape_profile orelse artifact_product.KernelCallShapeProfile{
37 .name = "philox",
38 .fingerprint = family_fingerprint,
39 .dimensions = shape_profile_dimensions[0..],
40 };
41
42 var graph = switch (instance.dtype) {
43 .f32 => try PhiloxRuntimeFamilyF32.buildNamed(allocator, options.limits, entry_name, instance),
44 .i32 => try PhiloxRuntimeFamilyI32.buildNamed(allocator, options.limits, entry_name, instance),
45 else => return error.UnsupportedDType,
46 };
47 defer graph.deinit();
48 return kernel.createKernelCallArtifact(allocator, handle, &graph, .{
49 .target = target,
50 .version = philox_family_version,
51 .format = options.format,
52 .kernel_plan = options.kernel_plan,
53 .element_count_argument = options.element_count_argument,
54 .shape_family_fingerprint = family_fingerprint,
55 .shape_profile = shape_profile,
56 .launch = options.launch orelse try randomDerivedLaunch(instance.threads, philox_lanes),
57 .runtime_scalar_argument_count = if (options.runtime_scalar_argument_count == 0) 3 else options.runtime_scalar_argument_count,
58 .static_arguments = options.static_arguments,
59 });
60 }