Skip to content

Commit

Permalink
added feature exctraction API to access weight_index and value
Browse files Browse the repository at this point in the history
allow for uninitialized Thread Pool
  • Loading branch information
eisber committed Mar 30, 2016
1 parent 3b46377 commit 145ff05
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 20 deletions.
2 changes: 0 additions & 2 deletions cs/cli/vw_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ license as described in the file LICENSE.

#include "vw_builder.h"
#include "parser.h"
// #include "primitives.h"

namespace VW
{
Expand Down Expand Up @@ -118,7 +117,6 @@ namespace VW
}
weight_index_base++;
}

}

void VowpalWabbitNamespaceBuilder::AddFeature(uint64_t weight_index, float x)
Expand Down
137 changes: 137 additions & 0 deletions cs/cli/vw_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,141 @@ namespace VW

return nullptr;
}

System::Collections::IEnumerator^ VowpalWabbitExample::EnumerableGetEnumerator::get()
{
return GetEnumerator();
}

IEnumerator<VowpalWabbitNamespace^>^ VowpalWabbitExample::GetEnumerator()
{
return gcnew NamespaceEnumerator(m_example);
}

VowpalWabbitExample::NamespaceEnumerator::NamespaceEnumerator(example* example)
: m_example(example)
{
Reset();
}

VowpalWabbitExample::NamespaceEnumerator::~NamespaceEnumerator()
{ }

bool VowpalWabbitExample::NamespaceEnumerator::MoveNext()
{
m_current++;

return m_current < m_example->indices.end();
}

void VowpalWabbitExample::NamespaceEnumerator::Reset()
{
// position before the beginning.
m_current = m_example->indices.begin() - 1;
}

VowpalWabbitNamespace^ VowpalWabbitExample::NamespaceEnumerator::Current::get()
{
if (m_current < m_example->indices.begin() || m_current >= m_example->indices.end())
throw gcnew InvalidOperationException();

return gcnew VowpalWabbitNamespace(*m_current, &m_example->feature_space[*m_current]);
}

System::Object^ VowpalWabbitExample::NamespaceEnumerator::IEnumeratorCurrent::get()
{
return Current;
}

VowpalWabbitFeature::VowpalWabbitFeature(feature_value x, uint64_t weight_index)
: m_x(x), m_weight_index(weight_index)
{ }

float VowpalWabbitFeature::X::get()
{
return m_x;
}

uint64_t VowpalWabbitFeature::WeightIndex::get()
{
return m_weight_index;
}

bool VowpalWabbitFeature::Equals(Object^ o)
{
VowpalWabbitFeature^ other = dynamic_cast<VowpalWabbitFeature^>(o);

return other != nullptr &&
other->m_x == m_x &&
other->m_weight_index == m_weight_index;
}

int VowpalWabbitFeature::GetHashCode()
{
return (int)m_x + m_weight_index;
}


VowpalWabbitNamespace::VowpalWabbitNamespace(namespace_index ns, features* features)
: m_ns(ns), m_features(features)
{ }

VowpalWabbitNamespace::~VowpalWabbitNamespace()
{ }

namespace_index VowpalWabbitNamespace::Index::get()
{
return m_ns;
}

System::Collections::IEnumerator^ VowpalWabbitNamespace::EnumerableGetEnumerator::get()
{
return GetEnumerator();
}

IEnumerator<VowpalWabbitFeature^>^ VowpalWabbitNamespace::GetEnumerator()
{
return gcnew FeatureEnumerator(m_features);
}

VowpalWabbitNamespace::FeatureEnumerator::FeatureEnumerator(features* features)
: m_features(features), m_iterator(nullptr)
{
m_end = new Holder<features::iterator>{ features->end() };
}

VowpalWabbitNamespace::FeatureEnumerator::~FeatureEnumerator()
{
delete m_end;
delete m_iterator;
}

void VowpalWabbitNamespace::FeatureEnumerator::Reset()
{
delete m_iterator;
m_iterator = nullptr;
}

bool VowpalWabbitNamespace::FeatureEnumerator::MoveNext()
{
if (m_iterator)
++m_iterator->value;
else
m_iterator = new Holder<features::iterator>{ m_features->begin() };

return m_iterator->value != m_end->value;
}

System::Object^ VowpalWabbitNamespace::FeatureEnumerator::IEnumeratorCurrent::get()
{
return Current;
}

VowpalWabbitFeature^ VowpalWabbitNamespace::FeatureEnumerator::Current::get()
{
if (!m_iterator || m_iterator->value == m_end->value)
throw gcnew InvalidOperationException();

return gcnew VowpalWabbitFeature(m_iterator->value.value(), m_iterator->value.index());
}
}
120 changes: 119 additions & 1 deletion cs/cli/vw_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,99 @@ license as described in the file LICENSE.

namespace VW
{
using namespace System::Collections::Generic;

[System::Diagnostics::DebuggerDisplay("{m_weight_index}:{m_x}")]
public ref struct VowpalWabbitFeature
{
private:
feature_value m_x;
uint64_t m_weight_index;

public:
VowpalWabbitFeature(feature_value x, uint64_t weight_index);

property feature_value X
{
float get();
}

property uint64_t WeightIndex
{
uint64_t get();
}

virtual bool Equals(Object^ o) override;

virtual int GetHashCode() override;
};

template <typename T>
struct Holder
{
T value;
};

[System::Diagnostics::DebuggerDisplay("{Index} = '{(char)Index}'")]
public ref struct VowpalWabbitNamespace : public IEnumerable<VowpalWabbitFeature^>
{
private:
ref class FeatureEnumerator : public IEnumerator<VowpalWabbitFeature^>
{
private:
features* m_features;
Holder<features::iterator>* m_iterator;
Holder<features::iterator>* m_end;

internal:
FeatureEnumerator(features* features);
~FeatureEnumerator();

property System::Object^ IEnumeratorCurrent
{
virtual System::Object^ get() sealed = System::Collections::IEnumerator::Current::get;
}

public:
virtual bool MoveNext();

virtual void Reset();

property VowpalWabbitFeature^ Current
{
virtual VowpalWabbitFeature^ get();
}
};

namespace_index m_ns;
features* m_features;

property System::Collections::IEnumerator^ EnumerableGetEnumerator
{
virtual System::Collections::IEnumerator^ get() sealed = System::Collections::IEnumerable::GetEnumerator;
}

public:
VowpalWabbitNamespace(namespace_index ns, features* features);
~VowpalWabbitNamespace();

property namespace_index Index
{
namespace_index get();
}


virtual IEnumerator<VowpalWabbitFeature^>^ GetEnumerator();
};

/// <summary>
/// A CLR representation of a vowpal wabbit example.
/// </summary>
/// <remarks>
/// Underlying memory is allocated by native code, but examples are not part of the ring.
/// </remarks>
[System::Diagnostics::DebuggerDisplay("{m_string}")]
public ref class VowpalWabbitExample
public ref class VowpalWabbitExample : public IEnumerable<VowpalWabbitNamespace^>
{
private:
/// <summary>
Expand All @@ -28,6 +113,32 @@ namespace VW
/// <remarks>If this instance owns <see name="m_example"/> this is null.</remarks>
initonly VowpalWabbitExample^ m_innerExample;

ref class NamespaceEnumerator : public IEnumerator<VowpalWabbitNamespace^>
{
private:
example* m_example;
namespace_index* m_current;

internal:
NamespaceEnumerator(example* example);
~NamespaceEnumerator();

property System::Object^ IEnumeratorCurrent
{
virtual System::Object^ get() sealed = System::Collections::IEnumerator::Current::get;
}

public:
virtual bool MoveNext();

virtual void Reset();

property VowpalWabbitNamespace^ Current
{
virtual VowpalWabbitNamespace^ get();
}
};

protected:
/// <summary>
/// Returns native example data structure to owning instance.
Expand Down Expand Up @@ -113,5 +224,12 @@ namespace VW
String^ Diff(VowpalWabbit^ vw, VowpalWabbitExample^ other, IVowpalWabbitLabelComparator^ labelComparator);

void MakeEmpty(VowpalWabbit^ vw);

property System::Collections::IEnumerator^ EnumerableGetEnumerator
{
virtual System::Collections::IEnumerator^ get() sealed = System::Collections::IEnumerable::GetEnumerator;
}

virtual IEnumerator<VowpalWabbitNamespace^>^ GetEnumerator();
};
}
11 changes: 7 additions & 4 deletions cs/cs/ObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class ObjectFactory
/// <typeparam name="TSource">The disposable context needed to create objects of <typeparamref name="TObject"/>.</typeparam>
/// <typeparam name="TObject">The type of the objects to be created.</typeparam>
public static ObjectFactory<TSource, TObject> Create<TSource, TObject>(TSource context, Func<TSource, TObject> creator)
where TSource : IDisposable
where TSource : class, IDisposable
{
return new ObjectFactory<TSource,TObject>(context, creator);
}
Expand All @@ -33,7 +33,7 @@ public static ObjectFactory<TSource, TObject> Create<TSource, TObject>(TSource c
/// <typeparam name="TSource">The disposable context needed to create objects of <typeparamref name="TObject"/>.</typeparam>
/// <typeparam name="TObject">The type of the objects to be created.</typeparam>
public class ObjectFactory<TSource, TObject> : IDisposable
where TSource : IDisposable
where TSource : class, IDisposable
{
/// <summary>
/// Factory function to create new instances.
Expand Down Expand Up @@ -80,8 +80,11 @@ private void Dispose(bool disposing)
{
if (!disposed)
{
this.source.Dispose();
this.source = default(TSource);
if (this.source != null)
{
this.source.Dispose();
this.source = null;
}
this.disposed = true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions cs/cs/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace VW
/// <typeparam name="TSource">The disposable context needed to create objects of <typeparamref name="TObject"/>.</typeparam>
/// <typeparam name="TObject">The type of the objects to be created.</typeparam>
public class ObjectPool<TSource, TObject> : IDisposable
where TObject : IDisposable
where TSource : IDisposable
where TSource : class, IDisposable
where TObject : class, IDisposable
{
/// <summary>
/// Lock resources
Expand Down
8 changes: 5 additions & 3 deletions cs/cs/PooledObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace VW
/// <typeparam name="TSource">The disposable context needed to create objects of <typeparamref name="TObject"/>.</typeparam>
/// <typeparam name="TObject">The type of the objects to be created.</typeparam>
public sealed class PooledObject<TSource, TObject> : IDisposable
where TSource : IDisposable
where TObject : IDisposable
where TSource : class, IDisposable
where TObject : class, IDisposable
{
/// <summary>
/// The parent pool.
Expand Down Expand Up @@ -52,7 +52,9 @@ internal PooledObject(ObjectPool<TSource, TObject> pool, int version, TObject va
/// </summary>
public void Dispose()
{
this.pool.ReturnObject(this);
// don't keep empty objects in pool
if (this.Value != null)
this.pool.ReturnObject(this);
}
}
}
2 changes: 1 addition & 1 deletion cs/cs/VowpalWabbitJsonThreadedPrediction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class VowpalWabbitJsonThreadedPrediction : VowpalWabbitThreadedPre
/// Initializes a new instance of <see cref="VowpalWabbitThreadedPrediction{TExample}"/>.
/// </summary>
/// <param name="model">The model used by each pool instance.</param>
public VowpalWabbitJsonThreadedPrediction(VowpalWabbitModel model)
public VowpalWabbitJsonThreadedPrediction(VowpalWabbitModel model = null)
: base(model)
{
}
Expand Down
Loading

0 comments on commit 145ff05

Please sign in to comment.