@@ -337,3 +337,81 @@ def _gen_samples(
337337
338338 bin_file_name = f"{ dataset_dir } /example_{ label } _{ cl } _{ i } _i{ str (inp_idx ).zfill (2 )} .bin"
339339 dt .tofile (bin_file_name )
340+
341+
342+ class LinearRampDatasetCreator (DatasetCreator ):
343+ """Dataset creator that generates deterministic linear ramp input samples.
344+
345+ The generated data forms a monotonic sequence where values are evenly
346+ distributed between a specified range (low to high) and span the full
347+ interval. The first element is equal to `low` and the last element is
348+ equal to `high`, with increments depending on the total number of elements.
349+ """
350+
351+ def __init__ (self , num_samples = 2 , low = 0.0 , high = 1.0 ):
352+ self ._num_samples = num_samples
353+ self .low = low
354+ self .high = high
355+
356+ def generate_samples (
357+ self , dataset_dir : str , input_spec : list [ModelInputSpec ]
358+ ) -> tuple [str , str ]:
359+ assert isinstance (input_spec , list ) and all (
360+ isinstance (spec , ModelInputSpec ) for spec in input_spec
361+ ), "Input_spec must be a list of ModelInputSpec."
362+
363+ calibration_dir , test_dir = (
364+ _get_calibration_and_testing_dataset_directory_names (dataset_dir )
365+ )
366+
367+ if any (spec .dim_order == torch .channels_last for spec in input_spec ):
368+ # We will need to generate a separate testing dataset, containing the same data as is in the calibration
369+ # dataset, just permuted to channels last where necessary.
370+ self ._gen_samples (test_dir , input_spec )
371+
372+ else :
373+ # Use the calibration dataset for testing as well.
374+ test_dir = calibration_dir
375+
376+ # Make sure the calibration dataset contains contiguous tensors.
377+ contiguous_input_spec = deepcopy (input_spec )
378+ for spec in contiguous_input_spec :
379+ spec .dim_order = torch .contiguous_format
380+
381+ # Generate the calibration dataset. Calibration amd testing dataset s will contain
382+ # the same data (except for the permutation).
383+ self ._gen_samples (calibration_dir , contiguous_input_spec )
384+
385+ return calibration_dir , test_dir
386+
387+ def _gen_samples (self , dataset_dir : str , input_spec : list [ModelInputSpec ]):
388+ for idx in range (self ._num_samples ):
389+ sample_dir = dataset_dir
390+
391+ # Multi-input, use a subdirectory containing the inputs for each sample
392+ if len (input_spec ) > 1 :
393+ sample_dir = os .path .join (dataset_dir , f"{ str (idx ).zfill (4 )} " )
394+ mkdir (sample_dir )
395+
396+ for spec_idx , spec in enumerate (input_spec ):
397+ match spec .dim_order :
398+ case torch .contiguous_format :
399+ shape = spec .shape
400+ case torch .channels_last :
401+ shape = tuple (
402+ translator .dims_to_channels_last (list (spec .shape ))
403+ )
404+ case _:
405+ raise ValueError (f"Unsupported dim_order: { spec .dim_order } " )
406+
407+ sample_vector = (
408+ np .linspace (self .low , self .high , num = np .prod (shape ))
409+ .astype (torch_type_to_numpy_type (spec .dtype ))
410+ .reshape (shape )
411+ )
412+ file_name = (
413+ f"{ str (spec_idx ).zfill (2 )} .bin"
414+ if len (input_spec ) > 1
415+ else f"{ str (idx ).zfill (4 )} .bin"
416+ )
417+ sample_vector .tofile (os .path .join (sample_dir , file_name ))
0 commit comments