Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion FastText.NetWrapper/FastText.NetWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="FastText.Native.Linux" Version="1.0.115" />
<PackageReference Include="FastText.Native.MacOs" Version="1.0.115" />
<PackageReference Include="FastText.Native.Windows" Version="1.0.115" />
Expand Down
55 changes: 36 additions & 19 deletions FastText.NetWrapper/FastTextArgs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.InteropServices;
using AutoMapper;

namespace FastText.NetWrapper;

Expand Down Expand Up @@ -40,8 +39,8 @@ public unsafe QuantizedSupervisedArgs()
FastTextWrapper.FastTextArgsStruct* argsPtr;

GetDefaultSupervisedArgs(new IntPtr(&argsPtr));
Mapper.Map(*argsPtr, this);

MapFromStruct(*argsPtr);

DestroyArgs(new IntPtr(argsPtr));
}
Expand Down Expand Up @@ -87,7 +86,7 @@ public unsafe SupervisedArgs()

GetDefaultSupervisedArgs(new IntPtr(&argsPtr));

Mapper.Map(*argsPtr, this);
MapFromStruct(*argsPtr);

DestroyArgs(new IntPtr(argsPtr));
}
Expand Down Expand Up @@ -120,20 +119,6 @@ public abstract class FastTextArgs

#endregion

protected static readonly IMapper Mapper;

static FastTextArgs()
{
Mapper = new MapperConfiguration(config =>
{
config.ShouldMapProperty = prop => prop.GetMethod.IsPublic || prop.GetMethod.IsAssembly;
config.CreateMap<FastTextWrapper.FastTextArgsStruct, FastTextArgs>();
config.CreateMap<FastTextWrapper.FastTextArgsStruct, SupervisedArgs>();
config.CreateMap<FastTextWrapper.FastTextArgsStruct, UnsupervisedArgs>();
config.CreateMap<FastTextWrapper.FastTextArgsStruct, QuantizedSupervisedArgs>();
}).CreateMapper();
}

/// <summary>
/// This constructor gets values from
/// https://github.com/olegtarasov/fastText/blob/b0a32d744f4d16d8f9834649f6f178ff79b5a4ce/src/fasttext_api.cc#L12
Expand All @@ -146,11 +131,43 @@ protected unsafe FastTextArgs()

GetDefaultArgs(new IntPtr(&argsPtr));

Mapper.Map(*argsPtr, this);
MapFromStruct(*argsPtr);

DestroyArgs(new IntPtr(argsPtr));
}

protected void MapFromStruct(FastTextWrapper.FastTextArgsStruct argsStruct)
{
lr = argsStruct.lr;
lrUpdateRate = argsStruct.lrUpdateRate;
dim = argsStruct.dim;
ws = argsStruct.ws;
epoch = argsStruct.epoch;
minCount = argsStruct.minCount;
minCountLabel = argsStruct.minCountLabel;
neg = argsStruct.neg;
wordNgrams = argsStruct.wordNgrams;
loss = (LossName)argsStruct.loss;
model = (ModelName)argsStruct.model;
bucket = argsStruct.bucket;
minn = argsStruct.minn;
maxn = argsStruct.maxn;
thread = argsStruct.thread;
t = argsStruct.t;
verbose = argsStruct.verbose;
saveOutput = argsStruct.saveOutput;
seed = argsStruct.seed;

if (this is QuantizedSupervisedArgs quantized)
{
quantized.qout = argsStruct.qout;
quantized.retrain = argsStruct.retrain;
quantized.qnorm = argsStruct.qnorm;
quantized.cutoff = argsStruct.cutoff;
quantized.dsub = argsStruct.dsub;
}
}

/// <summary>
/// learning rate [0.1]
/// </summary>
Expand Down
82 changes: 54 additions & 28 deletions FastText.NetWrapper/FastTextWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Runtime.InteropServices;
using System.Text;
using AutoMapper;
using Microsoft.Extensions.Logging;

namespace FastText.NetWrapper;
Expand All @@ -12,7 +11,6 @@ public partial class FastTextWrapper : IDisposable
{
private static readonly Encoding _utf8 = Encoding.UTF8;

private readonly IMapper _mapper;
private readonly ILogger<FastTextWrapper> _logger;

private IntPtr _fastText;
Expand All @@ -25,17 +23,6 @@ public partial class FastTextWrapper : IDisposable
public FastTextWrapper(ILoggerFactory loggerFactory = null)
{
_logger = loggerFactory?.CreateLogger<FastTextWrapper>();

_mapper = new MapperConfiguration(config =>
{
config.ShouldMapProperty = prop => prop.GetMethod.IsPublic || prop.GetMethod.IsAssembly;
config.CreateMap<SupervisedArgs, FastTextArgsStruct>();
config.CreateMap<QuantizedSupervisedArgs, FastTextArgsStruct>();
config.CreateMap<UnsupervisedArgs, FastTextArgsStruct>();
config.CreateMap<AutotuneArgs, AutotuneArgsStruct>();
})
.CreateMapper();

_fastText = CreateFastText();
}

Expand All @@ -54,17 +41,6 @@ public FastTextWrapper(ILoggerFactory loggerFactory = null)
public FastTextWrapper(bool useBundledLibrary, ILoggerFactory loggerFactory = null)
{
_logger = loggerFactory?.CreateLogger<FastTextWrapper>();

_mapper = new MapperConfiguration(config =>
{
config.ShouldMapProperty = prop => prop.GetMethod.IsPublic || prop.GetMethod.IsAssembly;
config.CreateMap<SupervisedArgs, FastTextArgsStruct>();
config.CreateMap<QuantizedSupervisedArgs, FastTextArgsStruct>();
config.CreateMap<UnsupervisedArgs, FastTextArgsStruct>();
config.CreateMap<AutotuneArgs, AutotuneArgsStruct>();
})
.CreateMapper();

_fastText = CreateFastText();
}

Expand Down Expand Up @@ -220,10 +196,10 @@ internal void Supervised(string inputPath, string outputPath, SupervisedArgs arg

bool quantizeWithNoQuantTune = quantizedArgs != null && string.IsNullOrEmpty(autotuneArgs.ModelSize);

var argsStruct = _mapper.Map<FastTextArgsStruct>(args);
var argsStruct = ToArgsStruct(args);
argsStruct.model = model_name.sup;

var autotuneStruct = _mapper.Map<AutotuneArgsStruct>(autotuneArgs);
var autotuneStruct = ToAutotuneArgsStruct(autotuneArgs);
CheckForErrors(Train(
_fastText,
inputPath,
Expand Down Expand Up @@ -273,7 +249,7 @@ public void Unsupervised(UnsupervisedModel model, string inputPath, string outpu

args.model = (ModelName)model;

var argsStruct = _mapper.Map<FastTextArgsStruct>(args);
var argsStruct = ToArgsStruct(args);
CheckForErrors(Train(
_fastText,
inputPath,
Expand Down Expand Up @@ -305,7 +281,7 @@ public void Quantize(QuantizedSupervisedArgs args, string output = null)
if (string.IsNullOrEmpty(ModelPath) && string.IsNullOrEmpty(output))
throw new InvalidOperationException("Model was loaded from memory. You need to specify output path.");

var argsStruct = _mapper.Map<FastTextArgsStruct>(args);
var argsStruct = ToArgsStruct(args);
string outPath = AdjustPath(string.IsNullOrEmpty(output) ? ModelPath : output, true);

if ((Path.IsPathRooted(output) && !Directory.Exists(Path.GetDirectoryName(outPath))))
Expand Down Expand Up @@ -502,6 +478,56 @@ public void Dispose()
_fastText = IntPtr.Zero;
}

private static FastTextArgsStruct ToArgsStruct(FastTextArgs args)
{
var result = new FastTextArgsStruct
{
lr = args.lr,
lrUpdateRate = args.lrUpdateRate,
dim = args.dim,
ws = args.ws,
epoch = args.epoch,
minCount = args.minCount,
minCountLabel = args.minCountLabel,
neg = args.neg,
wordNgrams = args.wordNgrams,
loss = (loss_name)args.loss,
model = (model_name)args.model,
bucket = args.bucket,
minn = args.minn,
maxn = args.maxn,
thread = args.thread,
t = args.t,
verbose = args.verbose,
saveOutput = args.saveOutput,
seed = args.seed
};

if (args is QuantizedSupervisedArgs quantized)
{
result.qout = quantized.qout;
result.retrain = quantized.retrain;
result.qnorm = quantized.qnorm;
result.cutoff = quantized.cutoff;
result.dsub = quantized.dsub;
}

return result;
}

private static AutotuneArgsStruct ToAutotuneArgsStruct(AutotuneArgs args)
{
return new AutotuneArgsStruct
{
ValidationFile = args.ValidationFile,
Metric = args.Metric,
Predictions = args.Predictions,
Duration = args.Duration,
ModelSize = args.ModelSize,
Verbose = args.Verbose
};
}

private string AdjustPath(string path, bool isQuantized)
{
string result = Path.HasExtension(path) ? Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path)) : path;
Expand Down