* fix #6949

* add MoveForwardToAttribute to assembly.cs and fix search space sub namespace

---------

Co-authored-by: XiaoYun Zhang <xiaoyuz@microsoft.com>
This commit is contained in:
github-actions[bot] 2024-01-12 21:12:00 -07:00 коммит произвёл GitHub
Родитель a5b5c7a82a
Коммит ec498d87a2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 226 добавлений и 185 удалений

Просмотреть файл

@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Microsoft.ML.SearchSpace;
/// <summary>
/// Boolean choice attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class BooleanChoiceAttribute : Attribute
{
/// <summary>
/// Create a <see cref="BooleanChoiceAttribute"/>.
/// </summary>
public BooleanChoiceAttribute()
{
DefaultValue = true;
}
/// <summary>
/// Create a <see cref="BooleanChoiceAttribute"/> with default value.
/// </summary>
/// <param name="defaultValue">default value for this option.</param>
public BooleanChoiceAttribute(bool defaultValue)
{
DefaultValue = defaultValue;
}
public bool DefaultValue { get; }
}

Просмотреть файл

@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.Contracts;
using System.Linq;
namespace Microsoft.ML.SearchSpace;
/// <summary>
/// Choice attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class ChoiceAttribute : Attribute
{
/// <summary>
/// Create a <see cref="ChoiceAttribute"/> with <paramref name="candidates"/>.
/// </summary>
public ChoiceAttribute(params object[] candidates)
{
var candidatesType = candidates.Select(o => o.GetType()).Distinct();
Contract.Assert(candidatesType.Count() == 1, "multiple candidates type detected");
this.Candidates = candidates;
this.DefaultValue = null;
}
/// <summary>
/// Create a <see cref="ChoiceAttribute"/> with <paramref name="candidates"/> and <paramref name="defaultValue"/>.
/// </summary>
public ChoiceAttribute(object[] candidates, object defaultValue)
{
var candidatesType = candidates.Select(o => o.GetType()).Distinct();
Contract.Assert(candidatesType.Count() == 1, "multiple candidates type detected");
Contract.Assert(candidatesType.First() == defaultValue.GetType(), "candidates type doesn't match with defaultValue type");
this.Candidates = candidates;
this.DefaultValue = defaultValue;
}
/// <summary>
/// Get the candidates of this option.
/// </summary>
public object[] Candidates { get; }
/// <summary>
/// Get the default value of this option.
/// </summary>
public object DefaultValue { get; }
}

Просмотреть файл

@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Microsoft.ML.SearchSpace;
/// <summary>
/// attribution class for nest option.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class NestOptionAttribute : Attribute
{
/// <summary>
/// Create an <see cref="NestOptionAttribute"/>.
/// </summary>
public NestOptionAttribute()
{
}
}

Просмотреть файл

@ -0,0 +1,88 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Microsoft.ML.SearchSpace;
/// <summary>
/// Range attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class RangeAttribute : Attribute
{
/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(double min, double max, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = null;
this.LogBase = logBase;
}
/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(double min, double max, double init, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = init;
this.LogBase = logBase;
}
/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(int min, int max, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = null;
this.LogBase = logBase;
}
/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(int min, int max, int init, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = init;
this.LogBase = logBase;
}
/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(float min, float max, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = null;
this.LogBase = logBase;
}
/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(float min, float max, float init, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = init;
this.LogBase = logBase;
}
public object Min { get; }
public object Max { get; }
public object Init { get; }
public bool LogBase { get; }
}

Просмотреть файл

@ -8,3 +8,7 @@ using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.ML.SearchSpace.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")]
[assembly: InternalsVisibleTo("Microsoft.ML.AutoML, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")]
[assembly: TypeForwardedTo(typeof(Microsoft.ML.SearchSpace.BooleanChoiceAttribute))]
[assembly: TypeForwardedTo(typeof(Microsoft.ML.SearchSpace.ChoiceAttribute))]
[assembly: TypeForwardedTo(typeof(Microsoft.ML.SearchSpace.NestOptionAttribute))]
[assembly: TypeForwardedTo(typeof(Microsoft.ML.SearchSpace.RangeAttribute))]

Просмотреть файл

@ -1,37 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.Linq;
using Microsoft.ML.SearchSpace.Option;
namespace Microsoft.ML.SearchSpace
{
/// <summary>
/// Boolean choice attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class BooleanChoiceAttribute : Attribute
{
/// <summary>
/// Create a <see cref="BooleanChoiceAttribute"/>.
/// </summary>
public BooleanChoiceAttribute()
{
Option = new ChoiceOption(true, false);
}
/// <summary>
/// Create a <see cref="BooleanChoiceAttribute"/> with default value.
/// </summary>
/// <param name="defaultValue">default value for this option.</param>
public BooleanChoiceAttribute(bool defaultValue)
{
Option = new ChoiceOption(new object[] { true, false }, defaultChoice: defaultValue);
}
internal ChoiceOption Option { get; }
}
}

Просмотреть файл

@ -1,46 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using Microsoft.ML.SearchSpace.Option;
namespace Microsoft.ML.SearchSpace
{
/// <summary>
/// attribution class for <see cref="ChoiceOption"/>. The property or field it applys to will be treated as <see cref="ChoiceOption"/> in <see cref="SearchSpace{T}"/>.
/// <seealso cref="SearchSpace{T}"/>
/// <seealso cref="SearchSpace"/>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class ChoiceAttribute : Attribute
{
/// <summary>
/// Create a <see cref="ChoiceAttribute"/> with <paramref name="candidates"/>.
/// </summary>
public ChoiceAttribute(params object[] candidates)
{
var candidatesType = candidates.Select(o => o.GetType()).Distinct();
Contract.Assert(candidatesType.Count() == 1, "multiple candidates type detected");
Option = new ChoiceOption(candidates);
}
/// <summary>
/// Create a <see cref="ChoiceAttribute"/> with <paramref name="candidates"/> and <paramref name="defaultValue"/>.
/// </summary>
public ChoiceAttribute(object[] candidates, object defaultValue)
{
var candidatesType = candidates.Select(o => o.GetType()).Distinct();
Contract.Assert(candidatesType.Count() == 1, "multiple candidates type detected");
Contract.Assert(candidatesType.First() == defaultValue.GetType(), "candidates type doesn't match with defaultValue type");
Option = new ChoiceOption(candidates, defaultValue);
}
internal ChoiceOption Option { get; }
}
}

Просмотреть файл

@ -1,22 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Microsoft.ML.SearchSpace
{
/// <summary>
/// attribution class for nest option.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class NestOptionAttribute : Attribute
{
/// <summary>
/// Create an <see cref="NestOptionAttribute"/>.
/// </summary>
public NestOptionAttribute()
{
}
}
}

Просмотреть файл

@ -1,66 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.ML.SearchSpace.Option;
namespace Microsoft.ML.SearchSpace
{
/// <summary>
/// attribution class for <see cref="UniformDoubleOption"/>, <see cref="UniformSingleOption"/> and <see cref="UniformIntOption"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class RangeAttribute : Attribute
{
/// <summary>
/// Create a <see cref="RangeAttribute"/> for <see cref="UniformDoubleOption"/>.
/// </summary>
public RangeAttribute(double min, double max, bool logBase = false)
{
Option = new UniformDoubleOption(min, max, logBase);
}
/// <summary>
/// Create a <see cref="RangeAttribute"/> for <see cref="UniformDoubleOption"/>.
/// </summary>
public RangeAttribute(double min, double max, double init, bool logBase = false)
{
Option = new UniformDoubleOption(min, max, logBase, init);
}
/// <summary>
/// Create a <see cref="RangeAttribute"/> for <see cref="UniformIntOption"/>.
/// </summary>
public RangeAttribute(int min, int max, bool logBase = false)
{
Option = new UniformIntOption(min, max, logBase);
}
/// <summary>
/// Create a <see cref="RangeAttribute"/> for <see cref="UniformIntOption"/>.
/// </summary>
public RangeAttribute(int min, int max, int init, bool logBase = false)
{
Option = new UniformIntOption(min, max, logBase, init);
}
/// <summary>
/// Create a <see cref="RangeAttribute"/> for <see cref="UniformSingleOption"/>.
/// </summary>
public RangeAttribute(float min, float max, bool logBase = false)
{
Option = new UniformSingleOption(min, max, logBase);
}
/// <summary>
/// Create a <see cref="RangeAttribute"/> for <see cref="UniformSingleOption"/>.
/// </summary>
public RangeAttribute(float min, float max, float init, bool logBase = false)
{
Option = new UniformSingleOption(min, max, logBase, init);
}
internal OptionBase Option { get; }
}
}

Просмотреть файл

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
</ItemGroup>
</Project>

Просмотреть файл

@ -214,9 +214,18 @@ namespace Microsoft.ML.SearchSpace
OptionBase option = attributes.First() switch
{
ChoiceAttribute choice => choice.Option,
RangeAttribute range => range.Option,
BooleanChoiceAttribute booleanChoice => booleanChoice.Option,
ChoiceAttribute choice => choice.DefaultValue == null ? new ChoiceOption(choice.Candidates) : new ChoiceOption(choice.Candidates, defaultChoice: choice.DefaultValue),
RangeAttribute range => (range.Min, range.Max, range.Init, range.LogBase) switch
{
(double min, double max, double init, bool logBase) => new UniformDoubleOption(min, max, logBase, init),
(double min, double max, null, bool logBase) => new UniformDoubleOption(min, max, logBase),
(int min, int max, int init, bool logBase) => new UniformIntOption(min, max, logBase, init),
(int min, int max, null, bool logBase) => new UniformIntOption(min, max, logBase),
(float min, float max, float init, bool logBase) => new UniformSingleOption(min, max, logBase, init),
(float min, float max, null, bool logBase) => new UniformSingleOption(min, max, logBase),
_ => throw new NotImplementedException(),
},
BooleanChoiceAttribute booleanChoice => new ChoiceOption(new object[] { true, false }, defaultChoice: booleanChoice.DefaultValue),
NestOptionAttribute nest => GetSearchSpaceOptionFromType(field.FieldType),
_ => throw new NotImplementedException(),
};
@ -252,9 +261,18 @@ namespace Microsoft.ML.SearchSpace
OptionBase option = attributes.First() switch
{
ChoiceAttribute choice => choice.Option,
RangeAttribute range => range.Option,
BooleanChoiceAttribute booleanChoice => booleanChoice.Option,
ChoiceAttribute choice => choice.DefaultValue == null ? new ChoiceOption(choice.Candidates) : new ChoiceOption(choice.Candidates, defaultChoice: choice.DefaultValue),
RangeAttribute range => (range.Min, range.Max, range.Init, range.LogBase) switch
{
(double min, double max, double init, bool logBase) => new UniformDoubleOption(min, max, logBase, init),
(double min, double max, null, bool logBase) => new UniformDoubleOption(min, max, logBase),
(int min, int max, int init, bool logBase) => new UniformIntOption(min, max, logBase, init),
(int min, int max, null, bool logBase) => new UniformIntOption(min, max, logBase),
(float min, float max, float init, bool logBase) => new UniformSingleOption(min, max, logBase, init),
(float min, float max, null, bool logBase) => new UniformSingleOption(min, max, logBase),
_ => throw new NotImplementedException(),
},
BooleanChoiceAttribute booleanChoice => new ChoiceOption(new object[] { true, false }, defaultChoice: booleanChoice.DefaultValue),
NestOptionAttribute nest => GetSearchSpaceOptionFromType(property.PropertyType),
_ => throw new NotImplementedException(),
};
@ -274,7 +292,7 @@ namespace Microsoft.ML.SearchSpace
return;
}
if (attribute is RangeAttribute range && (range.Option is UniformDoubleOption || range.Option is UniformSingleOption))
if (attribute is RangeAttribute range && (range.Min is double || range.Min is float))
{
Contract.Assert(type != typeof(int) && type != typeof(short) && type != typeof(long), $"[Option:{optionName}] UniformDoubleOption or UniformSingleOption can't apply to property or field which type is int or short or long");
return;

Просмотреть файл

@ -10,10 +10,6 @@
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<ProjectReference Include="..\Microsoft.ML.CpuMath\Microsoft.ML.CpuMath.csproj" />
<ProjectReference Include="..\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />
<ProjectReference Include="..\Microsoft.ML.SearchSpace\Microsoft.ML.SearchSpace.csproj">
<PrivateAssets>all</PrivateAssets>
<IncludeInNuget>true</IncludeInNuget>
</ProjectReference>
</ItemGroup>
</Project>

Просмотреть файл

@ -12,6 +12,7 @@ using Microsoft.ML.TorchSharp.NasBert.Modules;
using Microsoft.ML.TorchSharp.Utils;
using TorchSharp;
using TorchSharp.Modules;
using static Microsoft.ML.TorchSharp.NasBert.Modules.SearchSpace;
namespace Microsoft.ML.TorchSharp.NasBert.Models
{
@ -255,13 +256,13 @@ namespace Microsoft.ML.TorchSharp.NasBert.Models
for (var i = 0; i < DistillBlocks; ++i)
{
var hiddenSizesPerBlock = Enumerable.Range(i * blockPerLayer, blockPerLayer)
.Select(j => SearchSpace.ArchHiddenSize[DiscreteArches[j]]).ToArray();
var nextHiddenSize = SearchSpace.CheckHiddenDimensionsAndReturnMax(hiddenSizesPerBlock);
.Select(j => ArchHiddenSize[DiscreteArches[j]]).ToArray();
var nextHiddenSize = CheckHiddenDimensionsAndReturnMax(hiddenSizesPerBlock);
if (nextHiddenSize == 0)
{
if (hiddenSizePerBlock.Count == 0)
{
nextHiddenSize = SearchSpace.ArchHiddenSize[^1];
nextHiddenSize = ArchHiddenSize[^1];
}
else
{