Skip to content

Commit

Permalink
Support string input for ofa export (#964) (#967)
Browse files Browse the repository at this point in the history
* support string input for ofa export
  • Loading branch information
LiuChiachi authored Jan 7, 2022
1 parent f4c1f48 commit e508373
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions paddleslim/nas/ofa/ofa.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@
DistillConfig.__new__.__defaults__ = (None, ) * len(DistillConfig._fields)


def to_tensor(string_values, name="text"):
"""
Create the tensor that the value holds the list of string.
NOTICE: The value will be holded in the cpu place.
Parameters:
string_values(list[string]): The value will be setted to the tensor.
name(string): The name of the tensor.
"""
tensor = paddle.Tensor(core.VarDesc.VarType.STRING, [], name,
core.VarDesc.VarType.STRINGS, False)
tensor.value().set_string_list(string_values)
return tensor


class OFABase(Layer):
def __init__(self, model):
super(OFABase, self).__init__()
Expand Down Expand Up @@ -531,6 +546,8 @@ def build_input(input_size, dtypes):
dtype = dtypes[0]
else:
dtype = dtypes
if dtype == core.VarDesc.VarType.STRINGS:
return to_tensor([""])
return paddle.cast(paddle.rand(list(input_size)), dtype)
if isinstance(input_size, dict):
inputs = {}
Expand Down
5 changes: 5 additions & 0 deletions paddleslim/quant/quanter.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def quant_post_static(
quantize_model_path,
batch_generator=None,
sample_generator=None,
data_loader=None,
model_filename=None,
params_filename=None,
save_model_filename='__model__',
Expand Down Expand Up @@ -345,6 +346,9 @@ def quant_post_static(
can be set. Beisdes, batch_generator supports lod tensor.
sample_generator(Python Generator): The sample generator provides
calibrate data for DataLoader, and it only returns a sample every time.
data_loader(Python Generator, Paddle.io.DataLoader, optional): The
Generator or Dataloader provides calibrate data, and it could
return a batch every time.
model_filename(str, optional): The name of model file. If parameters
are saved in separate files, set it as 'None'. Default: 'None'.
params_filename(str, optional): The name of params file.
Expand Down Expand Up @@ -398,6 +402,7 @@ def quant_post_static(
executor=executor,
sample_generator=sample_generator,
batch_generator=batch_generator,
data_loader=data_loader,
model_dir=model_dir,
model_filename=model_filename,
params_filename=params_filename,
Expand Down

0 comments on commit e508373

Please sign in to comment.